-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: export make request [EXT-6172] (#2538)
* feat: export make request [EXT-6172] * fix: prettier * fix: top level restAdapterParams export
- Loading branch information
1 parent
1bc30d7
commit 2cb7b6c
Showing
3 changed files
with
47 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { AxiosInstance } from 'contentful-sdk-core' | ||
import type { MakeRequestOptions, MakeRequestPayload } from '../../common-types' | ||
import type { OpPatch } from 'json-patch' | ||
import type { RawAxiosRequestHeaders } from 'axios' | ||
import endpoints from './endpoints' | ||
|
||
type makeAxiosRequest = MakeRequestOptions & { | ||
axiosInstance: AxiosInstance | ||
} | ||
export const makeRequest = async <R>({ | ||
axiosInstance, | ||
entityType, | ||
action: actionInput, | ||
params, | ||
payload, | ||
headers, | ||
userAgent, | ||
}: makeAxiosRequest): Promise<R> => { | ||
// `delete` is a reserved keyword. Therefore, the methods are called `del`. | ||
const action = actionInput === 'delete' ? 'del' : actionInput | ||
|
||
const endpoint: ( | ||
http: AxiosInstance, | ||
params?: Record<string, unknown>, | ||
payload?: Record<string, unknown> | OpPatch[] | MakeRequestPayload, | ||
headers?: RawAxiosRequestHeaders | ||
) => Promise<R> = | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
endpoints[entityType]?.[action] | ||
|
||
if (endpoint === undefined) { | ||
throw new Error('Unknown endpoint') | ||
} | ||
|
||
return await endpoint(axiosInstance, params, payload, { | ||
...headers, | ||
// overwrite the userAgent with the one passed in the request | ||
...(userAgent ? { 'X-Contentful-User-Agent': userAgent } : {}), | ||
}) | ||
} |
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