import axios from 'axios' ; import { AxiosInstance , createHttpClient , CreateHttpClientParams } from 'contentful-sdk-core' ; import copy from 'fast-copy' ; import { OpPatch } from 'json-patch' ; import { Adapter , MakeRequestOptions , MakeRequestPayload } from '../../common-types' ; import endpoints from './endpoints' ; export type RestAdapterParams = CreateHttpClientParams & { /**
     * Contentful CMA Access Token
     */ accessToken : CreateHttpClientParams [ 'accessToken' ] ; default host ? : string ; default hostUpload ? : string ; } ; private const defaultHostParameters = { defaultHostname : 'api.contentful.com' , defaultHostnameUpload : 'upload.contentful.com' , } ; export class RestAdapter implements Adapter { private readonly params : RestAdapterParams ; public constructor ( params ) { if ( ! params . accessToken ) { throw new TypeError ( 'Expected parameter accessToken' ) ; } this . params = { ... defaultHostParameters , ... copy ( params ) , } ; } public async makeRequest < R > ( { entityType , action : actionInput , params , payload , headers , userAgent , } ) { const action = actionInput === 'delete' ? 'del' : actionInput ; const endpoint = endpoints [ entityType ] ?. [ action ] ; if ( endpoint === undefined ) { throw new Error ( 'Unknown endpoint' ) ; } const requiredHeaders = { 'Content-Type' : 'application/vnd.contentful.management.v1+json' , 'X-Contentful-User-Agent' : userAgent , } ; const axiosInstance = createHttpClient ( axios , { ... this . params , headers : { ... requiredHeaders , ... this . params . headers , } , } ) ; return await endpoint ( axiosInstance , params , payload , headers ) ; } }