JsonApiClient
JSON:API Client class provides functionality specific to JSON:API server.
See
- JsonApiClientOptions
- BaseUrl
Extends
ApiClient
Constructors
new JsonApiClient()
Creates a new instance of the JsonApiClient.
Parameters
• baseUrl: string
The base URL of the API. BaseUrl
• options?: JsonApiClientOptions
(Optional) Additional options for configuring the API client. JsonApiClientOptions
Returns
Overrides
ApiClient.constructor
Source
json-api-client/src/JsonApiClient.ts:48
Properties
#private
Inherited from
ApiClient.#private
Source
api-client/dist/index.d.mts:208
apiPrefix
Inherited from
ApiClient.apiPrefix
Source
api-client/dist/index.d.mts:216
authentication
ApiClientOptions.authentication
Inherited from
ApiClient.authentication
Source
api-client/dist/index.d.mts:224
baseUrl
BaseUrl
Inherited from
ApiClient.baseUrl
Source
api-client/dist/index.d.mts:212
cache
Inherited from
ApiClient.cache
Source
api-client/dist/index.d.mts:232
customFetch
Inherited from
ApiClient.customFetch
Source
api-client/dist/index.d.mts:220
debug
Inherited from
ApiClient.debug
Source
api-client/dist/index.d.mts:244
decoupledRouterApiPrefix
See
JsonApiClientOptions.decoupledRouterApiPrefix
Source
json-api-client/src/JsonApiClient.ts:41
defaultLocale
ApiClientOptions.defaultLocale
Inherited from
ApiClient.defaultLocale
Source
api-client/dist/index.d.mts:228
indexLookup
See
JsonApiClientOptions.indexLookup
Source
json-api-client/src/JsonApiClient.ts:36
logger
Inherited from
ApiClient.logger
Source
api-client/dist/index.d.mts:240
router
link DecoupledRouterClient
Source
json-api-client/src/JsonApiClient.ts:31
serializer
Inherited from
ApiClient.serializer
Source
api-client/dist/index.d.mts:236
Methods
addAuthorizationHeader()
Adds an authorization header to the provided RequestInit options if authentication of type “Basic” is configured. If the authentication type is “OAuth”, it will fetch a new access token or use the stored access token if it exists and is still valid. if the authentication type is “Custom”, it will use the provided value.
Parameters
• options: undefined
| RequestInit
The RequestInit options to which the authorization header should be added.
Returns
Promise
<RequestInit
>
The updated RequestInit options with the authorization header, if applicable.
Inherited from
ApiClient.addAuthorizationHeader
Source
api-client/dist/index.d.mts:274
createResource()
Create a resource of the specified type using the provided body.
Type parameters
• T
Parameters
• type: string
The type of the entity with bundle information.
• body: string
| object
The body of the request.
• options?: CreateOptions
(Optional) Additional options for customizing the request. CreateOptions
Returns
Promise
<T
| RawApiResponseWithData
<T
>>
A Promise that resolves to a Response object or RawApiResponseWithData.
Remarks
This method initiates the creation of a resource by sending a POST request to the API.
Example
Source
json-api-client/src/JsonApiClient.ts:677
createURL()
Generates an endpoint URL based on the provided parameters.
Parameters
• __namedParameters: EndpointUrlSegments
Returns
Promise
<string
>
The endpoint URL as a string.
Params
params - The parameters to use for creating the URL. EndpointUrlSegments
Source
json-api-client/src/JsonApiClient.ts:223
deleteResource()
Deletes a resource of the specified type using the provided resource ID.
Type parameters
• T
Parameters
• type: string
The type of the entity with bundle information.
• resourceId: string
The ID of the resource to be deleted.
• options?: DeleteOptions
(Optional) Additional options for customizing the request. DeleteOptions
Returns
Promise
<T
| RawApiResponseWithData
<T
>>
A Promise that resolves to a Response object or RawApiResponseWithData.
Example
Source
json-api-client/src/JsonApiClient.ts:316
fetch()
Uses customFetch if it is set, otherwise uses the default fetch
Parameters
• input: RequestInfo
| URL
RequestInfo
• init?: RequestInit
RequestInit
Returns
Promise
<FetchReturn
>
a response wrapped in a promise
Inherited from
ApiClient.fetch
Source
api-client/dist/index.d.mts:257
getAccessToken()
Fetch the OAuth token from the BaseUrl
Parameters
• __namedParameters: OAuthCredentials
Returns
Promise
<OAuthTokenResponse
>
Inherited from
ApiClient.getAccessToken
Params
params - The credentials for getting an OAuth token. OAuthCredentials
Source
api-client/dist/index.d.mts:262
getCachedResponse()
Retrieves a cached response from the cache.
Type parameters
• T
Parameters
• cacheKey: string
The cache key to use for retrieving the cached response.
Returns
Promise
<null
| NonNullable
<Awaited
<T
>>>
A promise wrapping the cached response as a generic type.
Inherited from
ApiClient.getCachedResponse
Source
api-client/dist/index.d.mts:286
getCollection()
Retrieves a collection of data of a specific entity type and bundle from the JSON:API .
Type parameters
• T
Parameters
• type: string
The type of resource to retrieve, in the format “entityType—bundle”. For example, “node—page”. EntityTypeWithBundle
• options?: GetOptions
(Optional) Additional options for customizing the request. GetOptions
Returns
Promise
<T
| RawApiResponseWithData
<T
>>
A Promise that resolves to the JSON data of the requested resource.
Example
Using JSONAPI.CollectionResourceDoc type from the jsonapi-typescript package
Source
json-api-client/src/JsonApiClient.ts:83
getResource()
Retrieves data for a resource by ID of a specific entity type and bundle from the JSON:API .
Type parameters
• T
Parameters
• type: string
The type of resource to retrieve, often in the format “entityType—bundle”, but may be rewritten as a single string.
• resourceId: string
The ID of the individual resource to retrieve.
• options?: GetOptions
(Optional) Additional options for customizing the request. GetOptions
Returns
Promise
<T
| RawApiResponseWithData
<T
>>
A Promise that resolves to the JSON data of the requested resource.
Example
Using JSONAPI.CollectionResourceDoc type from the jsonapi-typescript package
Source
json-api-client/src/JsonApiClient.ts:156
getResourceByPath()
Attempts to resolve a path to a resource and then fetches the resolved resource.
Parameters
• path: string
The path to resolve and fetch.
• options?: GetOptions
(Optional) Additional options for customizing the request. GetOptions
Returns
Promise
<unknown
>
- A promise that resolves to either the JSON data of the requested resource or an UnResolvedPath if the path could not be resolved.
Example
Source
json-api-client/src/JsonApiClient.ts:523
getView()
Retrieves a collection of data of a specific view display via JSON:API views. Requires that the jsonapi_views module is enabled on your Drupal site.
Type parameters
• T
Parameters
• type: string
The type of resource to retrieve, in the format “name—display_id”. For example, “content—page_1”.
• options?: GetOptions
(Optional) Additional options for customizing the request. GetOptions
Returns
Promise
<T
| RawApiResponseWithData
<T
>>
A Promise that resolves to the JSON data of the requested collection.
Example
Using JSONAPI.CollectionResourceDoc type from the jsonapi-typescript package
Source
json-api-client/src/JsonApiClient.ts:455
log()
Calls the appropriate logger method based on level
Parameters
• level: LogLevels
level based on npm log levels
• message: string
the message to log
Returns
void
Inherited from
ApiClient.log
Source
api-client/dist/index.d.mts:280
processApiResponseAndParseBody()
Processes the API response and returns the JSON data.
Type parameters
• T
Parameters
• response: Response
The response object from the API.
• cacheKey: string
The cache key to use for caching the response.
• options?: GetOptions
| UpdateOptions
| CreateOptions
(Optional) Additional options for customizing the request. CreateOptions
Returns
Promise
<string
| T
>
Source
json-api-client/src/JsonApiClient.ts:746
updateResource()
Updates a resource of the specified type using the provided resource ID.
Type parameters
• T
Parameters
• type: string
The type of the entity with bundle information.
• resourceId: string
The ID of the resource to be updated.
• body: string
| object
The body of the request.
• options?: UpdateOptions
(Optional) Additional options for customizing the request. UpdateOptions
Returns
Promise
<T
| RawApiResponseWithData
<T
>>
A Promise that resolves to a Response object or RawApiResponseWithData.
Remarks
This method initiates the update of a resource by sending a PATCH request to the API.
Example
Source
json-api-client/src/JsonApiClient.ts:576
createCacheKey()
Generates a cache key based on the provided parameters. If the cacheKey parameter is provided, it will be used as the cache key.
Parameters
• __namedParameters: EndpointUrlSegments
Returns
Promise
<string
>
A promise wrapping the generated cache key as a string.
Params
params - The parameters to use for generating the cache key. EndpointUrlSegments
Examples
Source
json-api-client/src/JsonApiClient.ts:406
getEntityTypeIdAndBundleId()
Parameters
• type: string
Returns
object
bundleId
entityTypeId
Source
json-api-client/src/JsonApiClient.ts:379