Skip to content

Commit

Permalink
feat: export make request [EXT-6172] (#2538)
Browse files Browse the repository at this point in the history
* feat: export make request [EXT-6172]

* fix: prettier

* fix: top level restAdapterParams export
  • Loading branch information
BobHemphill76 authored Feb 4, 2025
1 parent 1bc30d7 commit 2cb7b6c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
41 changes: 41 additions & 0 deletions lib/adapters/REST/make-request.ts
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 } : {}),
})
}
38 changes: 4 additions & 34 deletions lib/adapters/REST/rest-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import type { RawAxiosRequestHeaders } from 'axios'
import axios from 'axios'
import type { AxiosInstance, CreateHttpClientParams } from 'contentful-sdk-core'
import { createHttpClient } from 'contentful-sdk-core'
import copy from 'fast-copy'
import type { OpPatch } from 'json-patch'
import type { Adapter, MakeRequestOptions, MakeRequestPayload } from '../../common-types'
import endpoints from './endpoints'
import type { Adapter, MakeRequestOptions } from '../../common-types'
import { makeRequest } from './make-request'

export type RestAdapterParams = CreateHttpClientParams & {
/**
Expand Down Expand Up @@ -65,35 +63,7 @@ export class RestAdapter implements Adapter {
})
}

public async makeRequest<R>({
entityType,
action: actionInput,
params,
payload,
headers,
userAgent,
}: MakeRequestOptions): 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(this.axiosInstance, params, payload, {
...headers,
// overwrite the userAgent with the one passed in the request
...(userAgent ? { 'X-Contentful-User-Agent': userAgent } : {}),
})
public async makeRequest<R>(opts: MakeRequestOptions): Promise<R> {
return makeRequest({ ...opts, axiosInstance: this.axiosInstance })
}
}
2 changes: 2 additions & 0 deletions lib/contentful-management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export { isDraft, isPublished, isUpdated } from './plain/checks'
export type { PlainClientAPI } from './plain/common-types'
export { createClient }
export { RestAdapter } from './adapters/REST/rest-adapter'
export type { RestAdapterParams } from './adapters/REST/rest-adapter'
export { makeRequest } from './adapters/REST/make-request'
export { editorInterfaceDefaults }
export type PlainClientDefaultParams = DefaultParams
export * from './export-types'
Expand Down

0 comments on commit 2cb7b6c

Please sign in to comment.