From 3371b60cd539f6fce8a31b29ed5b40b56abe9ecf Mon Sep 17 00:00:00 2001 From: Lizards Date: Mon, 20 May 2024 18:52:15 -0400 Subject: [PATCH] geoblocking --- packages/fetch-api/src/fetch-error.ts | 2 ++ packages/fetch-api/src/index.ts | 2 +- packages/fetch-api/src/request.ts | 14 +++++++------- packages/fetch-api/src/result-types.ts | 1 + 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/fetch-api/src/fetch-error.ts b/packages/fetch-api/src/fetch-error.ts index 2be6dd30..d94c93e3 100644 --- a/packages/fetch-api/src/fetch-error.ts +++ b/packages/fetch-api/src/fetch-error.ts @@ -10,4 +10,6 @@ class FetchError extends Error { } } +export class GeoFetchError extends FetchError {} + export default FetchError diff --git a/packages/fetch-api/src/index.ts b/packages/fetch-api/src/index.ts index f56c5d52..c70d3c63 100644 --- a/packages/fetch-api/src/index.ts +++ b/packages/fetch-api/src/index.ts @@ -2,7 +2,7 @@ import { appendGiphySDKRequestHeader, getGiphySDKRequestHeaders } from '@giphy/j export { default as GiphyFetch } from './api' export { serverUrl, setServerUrl } from './constants' -export { default as FetchError } from './fetch-error' +export { default as FetchError, GeoFetchError } from './fetch-error' export * from './option-types' export { gifPaginator } from './paginator' export { default as request } from './request' diff --git a/packages/fetch-api/src/request.ts b/packages/fetch-api/src/request.ts index 41738b47..cbc1ee84 100644 --- a/packages/fetch-api/src/request.ts +++ b/packages/fetch-api/src/request.ts @@ -1,5 +1,5 @@ import { serverUrl } from './constants' -import FetchError from './fetch-error' +import FetchError, { GeoFetchError } from './fetch-error' import { ErrorResult, Result } from './result-types' export const ERROR_PREFIX = `@giphy/js-fetch-api: ` @@ -60,6 +60,7 @@ function request(url: string, options: RequestOptions = {}) { // error results have a different format than regular results const result = (await response.json()) as ErrorResult if (result.message) message = result.message + if (result.meta?.msg) message = result.meta.msg } catch (_) {} if (requestMap[url]) { // we got a specific error, @@ -69,12 +70,11 @@ function request(url: string, options: RequestOptions = {}) { } // we got an error response, throw with the message in the response body json - fetchError = new FetchError( - `${ERROR_PREFIX}${message}`, - fullUrl, - response.status, - response.statusText - ) + let Cls = FetchError + if (message === 'This content is not available in your location') { + Cls = GeoFetchError + } + fetchError = new Cls(`${ERROR_PREFIX}${message}`, fullUrl, response.status, response.statusText) } } catch (unexpectedError: any) { fetchError = new FetchError(unexpectedError.message, fullUrl) diff --git a/packages/fetch-api/src/result-types.ts b/packages/fetch-api/src/result-types.ts index 89245c28..6954715e 100644 --- a/packages/fetch-api/src/result-types.ts +++ b/packages/fetch-api/src/result-types.ts @@ -19,6 +19,7 @@ export interface Result { export interface ErrorResult { message?: string + meta?: ResultMeta } export interface GifResult extends Result { data: IGif