From 153050a0a4d82e813aa6bf425c698745baa011df Mon Sep 17 00:00:00 2001 From: JORGE Date: Thu, 12 Dec 2024 11:57:45 -0400 Subject: [PATCH] [TM-1531] apiSlice add job resource --- package.json | 2 +- .../v3/jobService/jobServiceFetcher.ts | 100 +----------------- src/store/apiSlice.ts | 4 +- 3 files changed, 9 insertions(+), 97 deletions(-) diff --git a/package.json b/package.json index b8bcf0a3d..602dceae6 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "generate:api": "openapi-codegen gen api", "generate:jobService": "openapi-codegen gen jobService", "generate:userService": "openapi-codegen gen userService", - "generate:services": "npm run generate:userService", + "generate:services": "yarn generate:userService && yarn generate:jobService", "tx:push": "eval $(grep '^TRANSIFEX_TOKEN' .env) && eval $(grep '^TRANSIFEX_SECRET' .env) && txjs-cli push --key-generator=hash src/ --token=$TRANSIFEX_TOKEN --secret=$TRANSIFEX_SECRET", "tx:pull": "eval $(grep '^TRANSIFEX_TOKEN' .env) && eval $(grep '^TRANSIFEX_SECRET' .env) && txjs-cli pull --token=$TRANSIFEX_TOKEN --secret=$TRANSIFEX_SECRET" }, diff --git a/src/generated/v3/jobService/jobServiceFetcher.ts b/src/generated/v3/jobService/jobServiceFetcher.ts index 83ccd35dd..97ea202d3 100644 --- a/src/generated/v3/jobService/jobServiceFetcher.ts +++ b/src/generated/v3/jobService/jobServiceFetcher.ts @@ -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 | { status: "unknown"; payload: string }; - -export type JobServiceFetcherOptions = { - 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): Promise { - 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; - 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 = {}, pathParams: Record = {}) => { - 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"; diff --git a/src/store/apiSlice.ts b/src/store/apiSlice.ts index b4bd732f8..be6362aa9 100644 --- a/src/store/apiSlice.ts +++ b/src/store/apiSlice.ts @@ -5,6 +5,7 @@ import { HYDRATE } from "next-redux-wrapper"; import { Store } from "redux"; import { setAccessToken } from "@/admin/apiProvider/utils/token"; +import { DelayedJobDto } from "@/generated/v3/jobService/jobServiceSchemas"; import { LoginDto, OrganisationDto, UserDto } from "@/generated/v3/userService/userServiceSchemas"; export type PendingErrorState = { @@ -53,12 +54,13 @@ type StoreResourceMap = Record; organisations: StoreResourceMap; users: StoreResourceMap; + delayedJobs: StoreResourceMap; }; export type JsonApiResource = {