-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa5b7e6
commit 153050a
Showing
3 changed files
with
9 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,6 @@ | ||
export type JobServiceFetcherExtraProps = { | ||
/** | ||
* You can add some extra props to your generated fetchers. | ||
* | ||
* Note: You need to re-gen after adding the first property to | ||
* have the `JobServiceFetcherExtraProps` injected in `JobServiceComponents.ts` | ||
**/ | ||
}; | ||
// This type is imported in the auto generated `jobServiceComponents` file, so it needs to be | ||
// exported from this file. | ||
export type { ErrorWrapper } from "../utils"; | ||
|
||
const baseUrl = ""; // TODO add your baseUrl | ||
|
||
export type ErrorWrapper<TError> = TError | { status: "unknown"; payload: string }; | ||
|
||
export type JobServiceFetcherOptions<TBody, THeaders, TQueryParams, TPathParams> = { | ||
url: string; | ||
method: string; | ||
body?: TBody; | ||
headers?: THeaders; | ||
queryParams?: TQueryParams; | ||
pathParams?: TPathParams; | ||
signal?: AbortSignal; | ||
} & JobServiceFetcherExtraProps; | ||
|
||
export async function jobServiceFetch< | ||
TData, | ||
TError, | ||
TBody extends {} | FormData | undefined | null, | ||
THeaders extends {}, | ||
TQueryParams extends {}, | ||
TPathParams extends {} | ||
>({ | ||
url, | ||
method, | ||
body, | ||
headers, | ||
pathParams, | ||
queryParams, | ||
signal | ||
}: JobServiceFetcherOptions<TBody, THeaders, TQueryParams, TPathParams>): Promise<TData> { | ||
try { | ||
const requestHeaders: HeadersInit = { | ||
"Content-Type": "application/json", | ||
...headers | ||
}; | ||
|
||
/** | ||
* As the fetch API is being used, when multipart/form-data is specified | ||
* the Content-Type header must be deleted so that the browser can set | ||
* the correct boundary. | ||
* https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects#sending_files_using_a_formdata_object | ||
*/ | ||
if (requestHeaders["Content-Type"].toLowerCase().includes("multipart/form-data")) { | ||
delete requestHeaders["Content-Type"]; | ||
} | ||
|
||
const response = await window.fetch(`${baseUrl}${resolveUrl(url, queryParams, pathParams)}`, { | ||
signal, | ||
method: method.toUpperCase(), | ||
body: body ? (body instanceof FormData ? body : JSON.stringify(body)) : undefined, | ||
headers: requestHeaders | ||
}); | ||
if (!response.ok) { | ||
let error: ErrorWrapper<TError>; | ||
try { | ||
error = await response.json(); | ||
} catch (e) { | ||
error = { | ||
status: "unknown" as const, | ||
payload: e instanceof Error ? `Unexpected error (${e.message})` : "Unexpected error" | ||
}; | ||
} | ||
|
||
throw error; | ||
} | ||
|
||
if (response.headers.get("content-type")?.includes("json")) { | ||
return await response.json(); | ||
} else { | ||
// if it is not a json response, assume it is a blob and cast it to TData | ||
return (await response.blob()) as unknown as TData; | ||
} | ||
} catch (e) { | ||
let errorObject: Error = { | ||
name: "unknown" as const, | ||
message: e instanceof Error ? `Network error (${e.message})` : "Network error", | ||
stack: e as string | ||
}; | ||
throw errorObject; | ||
} | ||
} | ||
|
||
const resolveUrl = (url: string, queryParams: Record<string, string> = {}, pathParams: Record<string, string> = {}) => { | ||
let query = new URLSearchParams(queryParams).toString(); | ||
if (query) query = `?${query}`; | ||
return url.replace(/\{\w*\}/g, key => pathParams[key.slice(1, -1)]) + query; | ||
}; | ||
// The serviceFetch method is the shared fetch method for all service fetchers. | ||
export { serviceFetch as jobServiceFetch } from "../utils"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters