Skip to content

Commit

Permalink
geoblocking
Browse files Browse the repository at this point in the history
  • Loading branch information
Lizards committed May 21, 2024
1 parent 0380176 commit 3371b60
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
2 changes: 2 additions & 0 deletions packages/fetch-api/src/fetch-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ class FetchError extends Error {
}
}

export class GeoFetchError extends FetchError {}

export default FetchError
2 changes: 1 addition & 1 deletion packages/fetch-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
14 changes: 7 additions & 7 deletions packages/fetch-api/src/request.ts
Original file line number Diff line number Diff line change
@@ -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: `
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions packages/fetch-api/src/result-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface Result {

export interface ErrorResult {
message?: string
meta?: ResultMeta
}
export interface GifResult extends Result {
data: IGif
Expand Down

0 comments on commit 3371b60

Please sign in to comment.