Skip to content
Snippets Groups Projects
Commit f602272a authored by Coby Sher's avatar Coby Sher Committed by Brian Perry
Browse files

Honor locale option in all CRUD methods

parent 0fcfef1c
No related branches found
No related tags found
2 merge requests!71Canary -> Main: Trigger Release Automation,!50Honor locale option in all CRUD methods
Pipeline #103520 passed
---
"@drupal-api-client/json-api-client": patch
---
All CRUD methods now support the `locale` option
......@@ -236,12 +236,14 @@ export class JsonApiClient extends ApiClient {
resourceId: string,
options?: DeleteOptions,
): Promise<T | RawApiResponseWithData<T>> {
const localeSegment = options?.locale || this.defaultLocale;
const { entityTypeId, bundleId } =
JsonApiClient.getEntityTypeIdAndBundleId(type);
const apiUrl = this.createURL({
entityTypeId,
bundleId,
resourceId,
localeSegment,
});
const cacheKey = await JsonApiClient.createCacheKey({
......@@ -407,12 +409,14 @@ export class JsonApiClient extends ApiClient {
body: string | object,
options?: UpdateOptions,
): Promise<T | RawApiResponseWithData<T>> {
const localeSegment = options?.locale || this.defaultLocale;
const { entityTypeId, bundleId } =
JsonApiClient.getEntityTypeIdAndBundleId(type);
const apiUrl = this.createURL({
entityTypeId,
bundleId,
resourceId,
localeSegment,
});
const cacheKey = await JsonApiClient.createCacheKey({
......@@ -504,11 +508,13 @@ export class JsonApiClient extends ApiClient {
body: string | object,
options?: CreateOptions,
): Promise<T | RawApiResponseWithData<T>> {
const localeSegment = options?.locale || this.defaultLocale;
const { entityTypeId, bundleId } =
JsonApiClient.getEntityTypeIdAndBundleId(type);
const apiUrl = this.createURL({
entityTypeId,
bundleId,
localeSegment,
});
const cacheKey = await JsonApiClient.createCacheKey({
......
......
......@@ -4,6 +4,12 @@ import type { Locale } from "@drupal-api-client/api-client";
* Base options for customizing each request.
*/
export interface RequestBaseOptions {
/**
* The locale to use for the request.
* If not set, the default locale will be used.
* If no default locale is set, no locale will be used.
*/
locale?: Locale;
/**
* Indicates whether the raw HTTP response should be returned.
* When set to true, the response will not be parsed or processed, providing the raw, unaltered response from the server.
......@@ -25,12 +31,6 @@ export interface RequestBaseOptions {
* Options for customizing the get request.
*/
export interface GetOptions extends RequestBaseOptions {
/**
* The locale to use for the request.
* If not set, the default locale will be used.
* If no default locale is set, no locale will be used.
*/
locale?: Locale;
/**
* A URL encoded query string to append to the request.
* See {@link https://www.drupal.org/docs/core-modules-and-themes/core-modules/jsonapi-module/fetching-resources-get} for some examples of valid query strings.
......
......
......@@ -42,6 +42,18 @@ describe("JsonApiClient.createResource()", () => {
expect(JSON.stringify(json)).toEqual(JSON.stringify(nodePageCreate));
});
it("should honor the locale option", async () => {
const apiClient = new JsonApiClient(baseUrl, { debug: true });
const locale = "es";
const { response, json } = (await apiClient.createResource(
type,
nodePageCreateRequest,
{ rawResponse: true, locale },
)) as RawApiResponseWithData<typeof nodePageCreate>;
expect(response.status).toEqual(201);
expect(JSON.stringify(json)).toEqual(JSON.stringify(nodePageCreate));
});
it("should give 404 for invalid bundle type, when passed rawResponse as true and return response with 404 status and body", async () => {
const apiClient = new JsonApiClient(baseUrl, { debug: true });
const { response, json } = (await apiClient.createResource(
......
......
import { JsonApiClient } from "../src/JsonApiClient";
import { RawApiResponseWithData } from "../src";
import { JsonApiClient } from "../src/JsonApiClient";
import notFoundResponse from "./mocks/data/404.json";
const baseUrl = "https://dev-drupal-api-client-poc.pantheonsite.io";
const resourceId = "35f7cd32-2c54-49f2-8740-0b0ec2ba61f6";
const invalidResourceId = "35f7cd32-2c54-49f2-8740-0b0ec2ba61f7";
const type = "node--page";
const locale = "es";
describe("JsonApiClient.deleteResource()", () => {
it("should delete resource when passed rawResponse as true and return raw response with 204 status and body", async () => {
......@@ -19,6 +20,12 @@ describe("JsonApiClient.deleteResource()", () => {
expect(json).toEqual("");
});
it("should delete resource and honor locale when passed in via the options", async () => {
const apiClient = new JsonApiClient(baseUrl, { debug: true });
const json = await apiClient.deleteResource(type, resourceId, { locale });
expect(json).toEqual("");
});
it("should delete resource when passed rawResponse as false and return raw response with 204 status and body", async () => {
const apiClient = new JsonApiClient(baseUrl, { debug: true });
const json = (await apiClient.deleteResource(type, resourceId, {
......
......
import { RawApiResponseWithData } from "../src";
import { JsonApiClient } from "../src/JsonApiClient";
import nodePageUpdateRequest from "./mocks/data/node-page-update-request.json";
import nodePageUpdateResource200Response from "./mocks/data/node-page-update-resource-200-response.json";
import { RawApiResponseWithData } from "../src";
import nodePageUpdateResource404Response from "./mocks/data/node-page-update-resource-404-response.json";
const baseUrl = "https://dev-drupal-api-client-poc.pantheonsite.io";
......@@ -24,6 +24,17 @@ describe("JsonApiClient.updateResource()", () => {
);
});
it("should update resource and honor the locale when passed body with type as string and rawResponse as true and return response with 200 status and body", async () => {
const locale = "es";
const apiClient = new JsonApiClient(baseUrl, { debug: true });
const json = await apiClient.updateResource<
typeof nodePageUpdateResource200Response
>(type, resourceId, JSON.stringify(nodePageUpdateRequest), { locale });
expect(JSON.stringify(json)).toEqual(
JSON.stringify(nodePageUpdateResource200Response),
);
});
it("should update resource when passed body with type as string and rawResponse as false and return and body", async () => {
const apiClient = new JsonApiClient(baseUrl, { debug: true });
const json = await apiClient.updateResource(
......
......
......@@ -98,6 +98,15 @@ export default [
headers: request.headers,
}),
),
http.delete(
`${baseUrl}/${overrideLocale}/${apiPrefix}/node/page/35f7cd32-2c54-49f2-8740-0b0ec2ba61f6`,
({ request }) =>
new Response(null, {
status: 204,
statusText: "No content",
headers: request.headers,
}),
),
http.delete(
`${baseUrl}/${apiPrefix}/node/page/35f7cd32-2c54-49f2-8740-0b0ec2ba61f7`,
({ request }) =>
......@@ -136,13 +145,12 @@ export default [
statusText: "Ok",
headers: request.headers,
});
} else {
}
return new Response(JSON.stringify(unresolvedRecipe), {
status: 404,
statusText: "Not Found",
headers: request.headers,
});
}
}),
http.get(`${baseUrl}/${overrideLocale}/${routerPrefix}`, ({ request }) => {
const url = new URL(request.url);
......@@ -156,14 +164,22 @@ export default [
statusText: "Ok",
headers: request.headers,
});
} else {
}
return new Response(JSON.stringify(unresolvedRecipe), {
status: 404,
statusText: "Not Found",
headers: request.headers,
});
}
}),
http.patch(
`${baseUrl}/${overrideLocale}/${apiPrefix}/node/page/11fc449b-aca0-4b74-bc3b-677da021f1d7`,
({ request }) =>
new Response(JSON.stringify(nodePageUpdateResource200Response), {
status: 200,
statusText: "Ok",
headers: request.headers,
}),
),
http.patch(
`${baseUrl}/${apiPrefix}/node/page/11fc449b-aca0-4b74-bc3b-677da021f1d7`,
({ request }) =>
......@@ -192,6 +208,15 @@ export default [
headers: request.headers,
}),
),
http.post(
`${baseUrl}/${overrideLocale}/${apiPrefix}/node/page`,
({ request }) =>
new Response(JSON.stringify(nodePageCreate), {
status: 201,
statusText: "Created",
headers: request.headers,
}),
),
http.post(
`${baseUrl}/${apiPrefix}/node/invalid-bundle`,
({ request }) =>
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment