From 2926c73349c6468234d3db444cefdc64171d46b0 Mon Sep 17 00:00:00 2001 From: Ken Eucker Date: Sun, 6 Mar 2022 21:47:58 -0800 Subject: [PATCH] fix(imgur): resolves errors with the returning success of the deleteTag method for the imgur adapter --- src/client.ts | 6 ++++++ src/common/payloads.ts | 28 ++++++++++++++-------------- src/common/types.ts | 2 ++ src/imgur/deleteTag.ts | 5 ++++- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/client.ts b/src/client.ts index bf36a38..2d16554 100644 --- a/src/client.ts +++ b/src/client.ts @@ -144,6 +144,12 @@ export class BikeTagClient extends EventEmitter { // Only exclude PUT, PATCH and DELETE methods from cache methods: ['put', 'patch', 'delete'], }, + // Attempt reading stale cache data when response status is either 4xx or 5xx + readOnError: (error) => { + return error.response.status >= 400 && error.response.status < 600 + }, + // Deactivate `clearOnStale` option so that we can actually read stale cache data + clearOnStale: false, }, headers, responseType, diff --git a/src/common/payloads.ts b/src/common/payloads.ts index 54f6193..7005d0b 100644 --- a/src/common/payloads.ts +++ b/src/common/payloads.ts @@ -1,5 +1,5 @@ import { Tag } from './schema' -import { CommonData } from './types' +import { CommonPayloadData } from './types' export type SortOptions = 'new' | 'relevance' | 'hot' | 'top' | 'comments' @@ -13,13 +13,13 @@ export type getTagsPayload = { sort?: SortOptions limit?: number tagnumbers: number[] -} & CommonData +} & CommonPayloadData export type deleteTagsPayload = { slugs?: string[] tagnumbers: number[] tags?: Partial[] -} & CommonData +} & CommonPayloadData export type getTagPayload = { tagnumber: number @@ -31,7 +31,7 @@ export type getTagPayload = { account?: string slug: string fields?: string[] -} & CommonData +} & CommonPayloadData export type updateTagPayload = { hash?: string @@ -52,7 +52,7 @@ export type uploadTagImagePayload = { type: 'found' | 'mystery' slug?: string stream: ReadableStream -} & CommonData +} & CommonPayloadData export type ImgurUploadPayload = { image: string @@ -68,7 +68,7 @@ export type getGamePayload = { name: string hash?: string fields?: string[] -} & CommonData +} & CommonPayloadData export type getPlayersPayload = { fields?: string[] @@ -78,7 +78,7 @@ export type getPlayersPayload = { limit?: number account?: string subreddit?: string -} & CommonData +} & CommonPayloadData export type getPlayerPayload = { fields?: string[] @@ -86,41 +86,41 @@ export type getPlayerPayload = { hash?: string account?: string subreddit?: string -} & CommonData +} & CommonPayloadData export type getSettingPayload = { fields?: string[] slug?: string -} & CommonData +} & CommonPayloadData export type getSettingsPayload = { fields?: string[] sort?: SortOptions limit?: number slugs?: string[] -} & CommonData +} & CommonPayloadData export type getAmbassadorsPayload = { fields?: string[] limit?: number sort?: SortOptions slugs?: string[] -} & CommonData +} & CommonPayloadData export type getAmbassadorPayload = { fields?: string[] slug?: string -} & CommonData +} & CommonPayloadData export type importTagPayload = Pick< Tag, 'discussionUrl' | 'mysteryImageUrl' | 'foundImageUrl' | 'mentionUrl' > & - CommonData + CommonPayloadData export type getQueuePayload = { hash?: string queuehash?: string -} & CommonData +} & CommonPayloadData export type queueTagPayload = { queuehash?: string diff --git a/src/common/types.ts b/src/common/types.ts index 96f97b0..6a38700 100644 --- a/src/common/types.ts +++ b/src/common/types.ts @@ -79,6 +79,8 @@ export interface CommonData { concise?: boolean cached?: boolean } + +export type CommonPayloadData = CommonData export interface AccessToken { accessToken: string } diff --git a/src/imgur/deleteTag.ts b/src/imgur/deleteTag.ts index a31d364..962e249 100644 --- a/src/imgur/deleteTag.ts +++ b/src/imgur/deleteTag.ts @@ -12,6 +12,7 @@ export async function deleteTag( const hashes = [] const hasImageUrls = payload.foundImageUrl || payload.mysteryImageUrl let tag = payload + if (!hasImageUrls && (payload.tagnumber || payload.slug)) { tag = await this.getTags(payload.tagnumber ?? payload.slug) } @@ -37,7 +38,9 @@ export async function deleteTag( return { data: responses, - success: true, + success: responses.reduce((o, r) => { + return o && (typeof r === 'boolean') && !!r + }, true), source: AvailableApis[AvailableApis.imgur], status: HttpStatusCode.Ok, }