JsonApiClient
Defined in: packages/json-api-client/src/JsonApiClient.ts:27
JSON:API Client class provides functionality specific to JSON:API server.
- JsonApiClientOptions
- BaseUrl
Extends
Section titled “Extends”ApiClient
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new JsonApiClient(baseUrl, options?): JsonApiClient;Defined in: packages/json-api-client/src/JsonApiClient.ts:48
Creates a new instance of the JsonApiClient.
Parameters
Section titled “Parameters”baseUrl
Section titled “baseUrl”string
The base URL of the API. BaseUrl
options?
Section titled “options?”(Optional) Additional options for configuring the API client. JsonApiClientOptions
Returns
Section titled “Returns”JsonApiClient
Overrides
Section titled “Overrides”ApiClient.constructorProperties
Section titled “Properties”apiPrefix
Section titled “apiPrefix”apiPrefix: string | undefined;Defined in: packages/api-client/dist/index.d.mts:216
Inherited from
Section titled “Inherited from”ApiClient.apiPrefixauthentication
Section titled “authentication”authentication: Authentication | undefined;Defined in: packages/api-client/dist/index.d.mts:224
ApiClientOptions.authentication
Inherited from
Section titled “Inherited from”ApiClient.authenticationbaseUrl
Section titled “baseUrl”baseUrl: string;Defined in: packages/api-client/dist/index.d.mts:212
BaseUrl
Inherited from
Section titled “Inherited from”ApiClient.baseUrlcache: Cache | undefined;Defined in: packages/api-client/dist/index.d.mts:232
Inherited from
Section titled “Inherited from”ApiClient.cachecustomFetch
Section titled “customFetch”customFetch: | ((input, init?) => Promise<Response>) | undefined;Defined in: packages/api-client/dist/index.d.mts:220
Inherited from
Section titled “Inherited from”ApiClient.customFetchdebug: boolean | undefined;Defined in: packages/api-client/dist/index.d.mts:244
Inherited from
Section titled “Inherited from”ApiClient.debugdecoupledRouterApiPrefix
Section titled “decoupledRouterApiPrefix”decoupledRouterApiPrefix: string | undefined;Defined in: packages/json-api-client/src/JsonApiClient.ts:41
JsonApiClientOptions.decoupledRouterApiPrefix
defaultLocale
Section titled “defaultLocale”defaultLocale: string | undefined;Defined in: packages/api-client/dist/index.d.mts:228
ApiClientOptions.defaultLocale
Inherited from
Section titled “Inherited from”ApiClient.defaultLocaleindexLookup
Section titled “indexLookup”indexLookup: boolean | undefined;Defined in: packages/json-api-client/src/JsonApiClient.ts:36
JsonApiClientOptions.indexLookup
logger
Section titled “logger”logger: | { debug?: LogMethod; error?: LogMethod; http?: LogMethod; info?: LogMethod; silly?: LogMethod; verbose?: LogMethod; warn?: LogMethod;} | undefined;Defined in: packages/api-client/dist/index.d.mts:240
Inherited from
Section titled “Inherited from”ApiClient.loggerrouter
Section titled “router”router: DecoupledRouterClient;Defined in: packages/json-api-client/src/JsonApiClient.ts:31
link DecoupledRouterClient
serializer
Section titled “serializer”serializer: Serializer | undefined;Defined in: packages/api-client/dist/index.d.mts:236
Inherited from
Section titled “Inherited from”ApiClient.serializerMethods
Section titled “Methods”addAuthorizationHeader()
Section titled “addAuthorizationHeader()”addAuthorizationHeader(options): Promise<RequestInit>;Defined in: packages/api-client/dist/index.d.mts:274
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
Section titled “Parameters”options
Section titled “options”RequestInit | undefined
The RequestInit options to which the authorization header should be added.
Returns
Section titled “Returns”Promise<RequestInit>
The updated RequestInit options with the authorization header, if applicable.
Inherited from
Section titled “Inherited from”ApiClient.addAuthorizationHeadercreateResource()
Section titled “createResource()”createResource<T>( type, body, options?): Promise< | T| RawApiResponseWithData<T>>;Defined in: packages/json-api-client/src/JsonApiClient.ts:679
Create a resource of the specified type using the provided body.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”string
The type of the entity with bundle information.
string | object
The body of the request.
options?
Section titled “options?”(Optional) Additional options for customizing the request. CreateOptions
Returns
Section titled “Returns”Promise<
| T
| RawApiResponseWithData<T>>
A Promise that resolves to a Response object or RawApiResponseWithData.
Remarks
Section titled “Remarks”This method initiates the creation of a resource by sending a POST request to the API.
Example
Section titled “Example”const responseBody = await createResource("node--page", `{ "data": { "type": "node--page", "attributes": { "title": "My custom title", "body": { "value": "Custom value", "format": "plain_text" } } }}, { rawResponse: false });createURL()
Section titled “createURL()”createURL(__namedParameters): Promise<string>;Defined in: packages/json-api-client/src/JsonApiClient.ts:223
Generates an endpoint URL based on the provided parameters.
Parameters
Section titled “Parameters”__namedParameters
Section titled “__namedParameters”Returns
Section titled “Returns”Promise<string>
The endpoint URL as a string.
Params
Section titled “Params”params - The parameters to use for creating the URL. EndpointUrlSegments
deleteResource()
Section titled “deleteResource()”deleteResource<T>( type, resourceId, options?): Promise< | T| RawApiResponseWithData<T>>;Defined in: packages/json-api-client/src/JsonApiClient.ts:318
Deletes a resource of the specified type using the provided resource ID.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”string
The type of the entity with bundle information.
resourceId
Section titled “resourceId”string
The ID of the resource to be deleted.
options?
Section titled “options?”(Optional) Additional options for customizing the request. DeleteOptions
Returns
Section titled “Returns”Promise<
| T
| RawApiResponseWithData<T>>
A Promise that resolves to a Response object or RawApiResponseWithData.
Example
Section titled “Example”const responseBody = await deleteResource("node--page", "7cbb8b73-8bcb-4008-874e-2bd496bd419d", { rawResponse: false });fetch()
Section titled “fetch()”fetch(input, init?): Promise<FetchReturn>;Defined in: packages/api-client/dist/index.d.mts:257
Uses customFetch if it is set, otherwise uses the default fetch
Parameters
Section titled “Parameters”URL | RequestInfo
RequestInfo
RequestInit
RequestInit
Returns
Section titled “Returns”Promise<FetchReturn>
a response wrapped in a promise
Inherited from
Section titled “Inherited from”ApiClient.fetchgetAccessToken()
Section titled “getAccessToken()”protected getAccessToken(__namedParameters): Promise<OAuthTokenResponse>;Defined in: packages/api-client/dist/index.d.mts:262
Fetch the OAuth token from the BaseUrl
Parameters
Section titled “Parameters”__namedParameters
Section titled “__namedParameters”OAuthCredentials
Returns
Section titled “Returns”Promise<OAuthTokenResponse>
Params
Section titled “Params”params - The credentials for getting an OAuth token. OAuthCredentials
Inherited from
Section titled “Inherited from”ApiClient.getAccessTokengetCachedResponse()
Section titled “getCachedResponse()”getCachedResponse<T>(cacheKey): Promise< | NonNullable<Awaited<T>>| null>;Defined in: packages/api-client/dist/index.d.mts:286
Retrieves a cached response from the cache.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”cacheKey
Section titled “cacheKey”string
The cache key to use for retrieving the cached response.
Returns
Section titled “Returns”Promise<
| NonNullable<Awaited<T>>
| null>
A promise wrapping the cached response as a generic type.
Inherited from
Section titled “Inherited from”ApiClient.getCachedResponsegetCollection()
Section titled “getCollection()”getCollection<T>(type, options?): Promise< | T| RawApiResponseWithData<T>>;Defined in: packages/json-api-client/src/JsonApiClient.ts:83
Retrieves a collection of data of a specific entity type and bundle from the JSON:API.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”string
The type of resource to retrieve, in the format “entityType—bundle”. For example, “node—page”. EntityTypeWithBundle
options?
Section titled “options?”(Optional) Additional options for customizing the request. GetOptions
Returns
Section titled “Returns”Promise<
| T
| RawApiResponseWithData<T>>
A Promise that resolves to the JSON data of the requested resource.
Example
Section titled “Example”Using JSONAPI.CollectionResourceDoc type from the jsonapi-typescript package
const collection = await jsonApiClient.get<JSONAPI.CollectionResourceDoc<string, Recipe>>("node--recipe");getResource()
Section titled “getResource()”getResource<T>( type, resourceId, options?): Promise< | T| RawApiResponseWithData<T>>;Defined in: packages/json-api-client/src/JsonApiClient.ts:156
Retrieves data for a resource by ID of a specific entity type and bundle from the JSON:API.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”string
The type of resource to retrieve, often in the format “entityType—bundle”, but may be rewritten as a single string.
resourceId
Section titled “resourceId”string
The ID of the individual resource to retrieve.
options?
Section titled “options?”(Optional) Additional options for customizing the request. GetOptions
Returns
Section titled “Returns”Promise<
| T
| RawApiResponseWithData<T>>
A Promise that resolves to the JSON data of the requested resource.
Example
Section titled “Example”Using JSONAPI.CollectionResourceDoc type from the jsonapi-typescript package
const collection = await jsonApiClient.get<JSONAPI.CollectionResourceDoc<string, Recipe>>("node--recipe");getResourceByPath()
Section titled “getResourceByPath()”getResourceByPath(path, options?): Promise<unknown>;Defined in: packages/json-api-client/src/JsonApiClient.ts:525
Attempts to resolve a path to a resource and then fetches the resolved resource.
Parameters
Section titled “Parameters”string
The path to resolve and fetch.
options?
Section titled “options?”(Optional) Additional options for customizing the request. GetOptions
Returns
Section titled “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
Section titled “Example”const article = await jsonApiClient.getResourceByPath("/articles/give-it-a-go-and-grow-your-own-herbs");getView()
Section titled “getView()”getView<T>(type, options?): Promise< | T| RawApiResponseWithData<T>>;Defined in: packages/json-api-client/src/JsonApiClient.ts:457
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
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”string
The type of resource to retrieve, in the format “name—display_id”. For example, “content—page_1”.
options?
Section titled “options?”(Optional) Additional options for customizing the request. GetOptions
Returns
Section titled “Returns”Promise<
| T
| RawApiResponseWithData<T>>
A Promise that resolves to the JSON data of the requested collection.
Example
Section titled “Example”Using JSONAPI.CollectionResourceDoc type from the jsonapi-typescript package
const view = await jsonApiClient.getView<JSONAPI.CollectionResourceDoc<string, Recipe>>("recipes--page_1");log(level, message): void;Defined in: packages/api-client/dist/index.d.mts:280
Calls the appropriate logger method based on level
Parameters
Section titled “Parameters”LogLevels
level based on npm log levels
message
Section titled “message”string
the message to log
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”ApiClient.logprocessApiResponseAndParseBody()
Section titled “processApiResponseAndParseBody()”processApiResponseAndParseBody<T>( response, cacheKey,options?): Promise<string | T>;Defined in: packages/json-api-client/src/JsonApiClient.ts:748
Processes the API response and returns the JSON data.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”response
Section titled “response”The response object from the API.
cacheKey
Section titled “cacheKey”string
The cache key to use for caching the response.
options?
Section titled “options?”| GetOptions
| UpdateOptions
| CreateOptions
(Optional) Additional options for customizing the request. UpdateOptions | CreateOptions
Returns
Section titled “Returns”Promise<string | T>
updateResource()
Section titled “updateResource()”updateResource<T>( type, resourceId, body, options?): Promise< | T| RawApiResponseWithData<T>>;Defined in: packages/json-api-client/src/JsonApiClient.ts:578
Updates a resource of the specified type using the provided resource ID.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”string
The type of the entity with bundle information.
resourceId
Section titled “resourceId”string
The ID of the resource to be updated.
string | object
The body of the request.
options?
Section titled “options?”(Optional) Additional options for customizing the request. UpdateOptions
Returns
Section titled “Returns”Promise<
| T
| RawApiResponseWithData<T>>
A Promise that resolves to a Response object or RawApiResponseWithData.
Remarks
Section titled “Remarks”This method initiates the update of a resource by sending a PATCH request to the API.
Example
Section titled “Example”const responseBody = await updateResource("node--page", "7cbb8b73-8bcb-4008-874e-2bd496bd419d", `{ "data": { "type": "node--page", "id": "11fc449b-aca0-4b74-bc3b-677da021f1d7", "attributes": { "drupal_internal__nid": 2, "drupal_internal__vid": 3, "langcode": "en", "revision_log": null, "status": true, "title": "test 2" } }}`, { rawResponse: false });createCacheKey()
Section titled “createCacheKey()”static createCacheKey(__namedParameters): Promise<string>;Defined in: packages/json-api-client/src/JsonApiClient.ts:408
Generates a cache key based on the provided parameters. If the cacheKey parameter is provided, it will be used as the cache key.
Parameters
Section titled “Parameters”__namedParameters
Section titled “__namedParameters”Returns
Section titled “Returns”Promise<string>
A promise wrapping the generated cache key as a string.
Params
Section titled “Params”params - The parameters to use for generating the cache key. EndpointUrlSegments
Examples
Section titled “Examples”// Generate a cache key with entityTypeId and bundleId onlyconst key1 = await MyClass.createCacheKey('entity1', 'bundle1');// key1: 'entity1--bundle1'// Generate a cache key with entityTypeId, bundleId, localeSegment, and queryStringconst key2 = await MyClass.createCacheKey('entity2', 'bundle2', 'en-US', 'param1=value1¶m2=value2');// key2: 'en-US--entity2--bundle2--<sha256_hash_of_query_string>'getEntityTypeIdAndBundleId()
Section titled “getEntityTypeIdAndBundleId()”static getEntityTypeIdAndBundleId(type): object;Defined in: packages/json-api-client/src/JsonApiClient.ts:381
Parameters
Section titled “Parameters”string
Returns
Section titled “Returns”object
bundleId
Section titled “bundleId”bundleId: string;entityTypeId
Section titled “entityTypeId”entityTypeId: string;