Skip to content

Commit

Permalink
feature: modify json api (#557)
Browse files Browse the repository at this point in the history
* feature: modifiy json api

* fix: add test for option usage

* fix: test the api only

---------

Co-authored-by: mattienodj <[email protected]>
  • Loading branch information
kt-mattie-langenberg and mattienodj authored Jan 17, 2025
1 parent b011aed commit a47ddb7
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export async function createOne<
"POST",
`${config.baseUrl}/${config.schemaMap[schemaName].endpoint}`,
jsonApiResource,
config.fetchOptions,
)

return Promise.resolve({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export async function deleteOne<
await fetchJsonApi(
"DELETE",
`${config.baseUrl}/${config.schemaMap[schemaName].endpoint}/${id}`,
undefined,
config.fetchOptions,
)

return Promise.resolve()
Expand Down
2 changes: 2 additions & 0 deletions packages/rest-client-jsonapi/src/services/findAll/findAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export async function findAll<
const json = await fetchJsonApi<JsonApiResource[]>(
"GET",
`${config.baseUrl}/${config.schemaMap[schemaName].endpoint}${queryParams}`,
undefined,
config.fetchOptions,
)

return Promise.resolve([
Expand Down
2 changes: 2 additions & 0 deletions packages/rest-client-jsonapi/src/services/findOne/findOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export async function findOne<
const json = await fetchJsonApi<JsonApiResource>(
"GET",
`${config.baseUrl}/${config.schemaMap[schemaName].endpoint}/${query.id}${queryParams}`,
undefined,
config.fetchOptions,
)

return Promise.resolve({
Expand Down
12 changes: 10 additions & 2 deletions packages/rest-client-jsonapi/src/services/jsonapi/jsonapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ export type CreateJsonApiResource = Omit<JsonApiResource, "id">
export function jsonapi<
const TSchemas extends Record<string, RestClientSchema>,
const TSchemaName extends GetSchemaNames<TSchemas>,
>(baseUrl: string, schemaMap: TSchemas): RestClient<TSchemas, TSchemaName> {
>(
baseUrl: string,
schemaMap: TSchemas,
fetchOptions?: RequestInit,
): RestClient<TSchemas, TSchemaName> {
// Default `type` to `schemaMap` key if not set in `schemaMap`
const completeSchemaMap = Object.entries(schemaMap).reduce(
(acc, [key, value]) => {
Expand All @@ -58,7 +62,11 @@ export function jsonapi<
{} as RestClientSchemaMap,
)

const config = { baseUrl, schemaMap: completeSchemaMap }
const config = {
baseUrl,
schemaMap: completeSchemaMap,
fetchOptions,
}

return {
completeSchemaMap: completeSchemaMap as TSchemas,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export async function updateOne<
"id" in jsonApiResource ? jsonApiResource.id : null
}`,
jsonApiResource,
config.fetchOptions,
)

return Promise.resolve({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ describe("rest-client-jsonapi/services/utils/fetch", () => {
})
})

it("works with fetch options", async () => {
const customHeaders = new Headers({
Authorization: "Bearer test-token",
"Custom-Header": "test-value",
})

const data = await fetchJsonApi(
"GET",
`${baseUrl}/${schemaMap.Article.endpoint}`,
undefined,
{
headers: customHeaders,
credentials: "include",
},
)

expect(data).toEqual({
data: testData.data,
included: testData.included,
meta: testData.meta,
})
})

it("throws an error if the request fails", async () => {
const errors = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export async function fetchJsonApi<T>(
method: "GET" | "POST" | "PATCH" | "DELETE",
url: string,
body?: { [key: string]: any },
fetchOptions?: RequestInit,
): Promise<{
data: T
included?: JsonApiResource[]
Expand All @@ -17,8 +18,10 @@ export async function fetchJsonApi<T>(
method,
body: body ? JSON.stringify({ data: body }) : undefined,
headers: {
"Content-Type": "application/vnd.api+json",
...fetchOptions?.headers,
"Content-Type": "application/vnd.api+json", // This will override any Content-Type in fetchOptions
},
...fetchOptions,
})

if (!response.ok) {
Expand Down
6 changes: 6 additions & 0 deletions packages/rest-client/src/services/types/rest-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type RestClientSchemaMap = Record<
export interface RestClientConfig {
baseUrl: string
schemaMap: RestClientSchemaMap
fetchOptions?: RequestInit
}

// always return a Resource[] even if it's a single resource because
Expand All @@ -45,6 +46,7 @@ export interface RestClient<
schemaName: TSchemaName,
query: QueryList<GetSchemaFromName<TSchemas, TSchemaName>>,
baseFilter?: Filters,
fetchOptions?: RequestInit,
) => Promise<
[
Resources: { records: Resource[]; related: Resource[] },
Expand All @@ -55,20 +57,24 @@ export interface RestClient<
allSchemas: FinalSchemas,
schemaName: TSchemaName,
query: QueryOne<GetSchemaFromName<TSchemas, TSchemaName>>,
fetchOptions?: RequestInit,
) => Promise<{ record: Resource; related: Resource[] }>
createOne: (
allSchemas: FinalSchemas,
schemaName: TSchemaName,
data: CreateType<GetSchemaFromName<TSchemas, TSchemaName>>,
fetchOptions?: RequestInit,
) => Promise<{ record: Resource; related: Resource[] }>
updateOne: (
allSchemas: FinalSchemas,
schemaName: TSchemaName,
data: UpdateType<GetSchemaFromName<TSchemas, TSchemaName>>,
fetchOptions?: RequestInit,
) => Promise<{ record: Resource; related: Resource[] }>
deleteOne: (
allSchemas: FinalSchemas,
schemaName: TSchemaName,
id: string,
fetchOptions?: RequestInit,
) => Promise<void>
}

0 comments on commit a47ddb7

Please sign in to comment.