Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add client seo files #759

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/analytics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ transformIgnorePatterns: [
You just need to import and use what you need. All imports should be done from the root of the package like in the following example:

```js
import { FromParameterTypes } from '@farfetch/blackout-analytics';
import { FromParameterType } from '@farfetch/blackout-analytics';

console.log(FromParameterTypes.BAG);
console.log(FromParameterType.BAG);
```

## Contributing
Expand Down
13 changes: 13 additions & 0 deletions packages/client/src/__tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,18 @@ Object {
"Merchant": 3,
"Other": 4,
},
"SeoFileType": Object {
"Categories": "Categories",
"CustomContentTypes": "CustomContentTypes",
"Homepage": "Homepage",
"None": "None",
"Other": "Other",
"Pages": "Pages",
"Posts": "Posts",
"Products": "Products",
"Sets": "Sets",
"Sitemap": "Sitemap",
},
"SeoPageType": Object {
"0": "None",
"1": "Default",
Expand Down Expand Up @@ -1073,6 +1085,7 @@ Object {
"getReturnPickupRescheduleRequest": [Function],
"getReturnPickupRescheduleRequests": [Function],
"getReturnWorkflow": [Function],
"getSEOFiles": [Function],
"getSEOMetadata": [Function],
"getSearchContents": [Function],
"getSearchDidYouMean": [Function],
Expand Down
19 changes: 19 additions & 0 deletions packages/client/src/contents/__fixtures__/seoFiles.fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { rest, type RestHandler } from 'msw';
import type { SEOFiles } from '../types/index.js';

const path = '/api/content/v1/seoFiles';

const fixtures = {
get: {
success: (response: SEOFiles): RestHandler =>
rest.get(path, (_req, res, ctx) =>
res(ctx.status(200), ctx.json(response)),
),
failure: (): RestHandler =>
rest.get(path, (_req, res, ctx) =>
res(ctx.status(404), ctx.json({ message: 'stub error' })),
),
},
};

export default fixtures;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`SEO Files client getSEOFiles() should handle a client request error 1`] = `
Object {
"code": "-1",
"message": "stub error",
"name": "AxiosError",
"status": 404,
"transportLayerErrorCode": "ERR_BAD_REQUEST",
}
`;
42 changes: 42 additions & 0 deletions packages/client/src/contents/__tests__/getSEOFiles.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { getSEOFiles } from '../index.js';
import {
seoFilesData,
seoFilesQuery,
} from 'tests/__fixtures__/contents/seoFiles.fixtures.mjs';
import client from '../../helpers/client/index.js';
import fixtures from '../__fixtures__/seoFiles.fixtures.js';
import mswServer from '../../../tests/mswServer.js';

describe('SEO Files client', () => {
const expectedConfig = undefined;

beforeEach(() => {
jest.clearAllMocks();
});

describe('getSEOFiles()', () => {
const spy = jest.spyOn(client, 'get');

it('should handle a client request successfully', async () => {
mswServer.use(fixtures.get.success(seoFilesData));

await expect(getSEOFiles(seoFilesQuery)).resolves.toEqual(seoFilesData);

expect(spy).toHaveBeenCalledWith(
'/content/v1/seofiles?hostId=1234&name=siteSEOFiles&page=1&pageSize=60',
expectedConfig,
);
});

it('should handle a client request error', async () => {
mswServer.use(fixtures.get.failure());

await expect(getSEOFiles(seoFilesQuery)).rejects.toMatchSnapshot();

expect(spy).toHaveBeenCalledWith(
'/content/v1/seofiles?hostId=1234&name=siteSEOFiles&page=1&pageSize=60',
expectedConfig,
);
});
});
});
18 changes: 18 additions & 0 deletions packages/client/src/contents/getSEOFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { adaptError } from '../helpers/client/formatError.js';
import client from '../helpers/client/index.js';
import join from 'proper-url-join';
import type { Config } from '../types/index.js';
import type { GetSEOFilesQuery, SEOFiles } from './types/seoFiles.types.js';

const getSEOFiles = (
query: GetSEOFilesQuery,
config?: Config,
): Promise<SEOFiles> =>
client
.get(join('/content/v1/seofiles', { query }), config)
.then(response => response.data)
.catch(error => {
throw adaptError(error);
});

export default getSEOFiles;
1 change: 1 addition & 0 deletions packages/client/src/contents/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export { default as getContentPage } from './getContentPage.js';
export { default as getContentTypes } from './getContentTypes.js';
export { default as getSearchContents } from './getSearchContents.js';
export { default as getSEOMetadata } from './getSEOMetadata.js';
export { default as getSEOFiles } from './getSEOFiles.js';

export * from './types/index.js';
1 change: 1 addition & 0 deletions packages/client/src/contents/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './contents.types.js';
export * from './contentPage.types.js';
export * from './contentTypes.types.js';
export * from './seoMetadata.types.js';
export * from './seoFiles.types.js';
42 changes: 42 additions & 0 deletions packages/client/src/contents/types/seoFiles.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { Config, PagedResponse } from '../../types/index.js';

export type GetSEOFiles = (
query: GetSEOFilesQuery,
config?: Config,
) => Promise<SEOFiles>;
boliveira marked this conversation as resolved.
Show resolved Hide resolved

export enum SeoFileType {
None = 'None',
Sitemap = 'Sitemap',
Homepage = 'Homepage',
Pages = 'Pages',
Posts = 'Posts',
Products = 'Products',
Sets = 'Sets',
Other = 'Other',
CustomContentTypes = 'CustomContentTypes',
Categories = 'Categories',
}

export type GetSEOFilesQuery = {
// The name (also named as "slug" on legacy CMS)
name: string;
// The hostId
hostId: number;
// Number of the page to get, starting at 1. The default is 1.
page?: number;
// Size of each page, as a number between 1 and 180. The default is 60.
pageSize?: number;
};

export type SEOFiles = PagedResponse<SEOFile>;

export type SEOFile = {
name: string;
path?: string;
uploadDate: string;
hostId: number;
subfolderStructure?: string;
type: SeoFileType;
content?: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ Object {
"useSearchDidYouMean": [Function],
"useSearchIntents": [Function],
"useSearchSuggestions": [Function],
"useSeoFiles": [Function],
"useSeoMetadata": [Function],
"useSubscriptionPackages": [Function],
"useTopCategories": [Function],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { type BlackoutError, SeoFileType } from '@farfetch/blackout-client';
import { generateSEOFilesHash } from '@farfetch/blackout-redux';
import { mockInitialState } from './useSeoMetadata.fixtures.js';
import { seoFilesQuery } from 'tests/__fixtures__/contents/seoFiles.fixtures.mjs';

const contentSEOFilesHash = generateSEOFilesHash(seoFilesQuery);

export const mockSEOFilesState = {
contents: {
...mockInitialState.contents,
seoFiles: {
error: {},
isLoading: {},
result: {
[contentSEOFilesHash]: {
number: 1,
totalItems: 1,
totalPages: 1,
entries: [
{
name: 'string',
path: 'string',
uploadDate: '2023-05-23T17:47:02.770Z',
hostId: 0,
subfolderStructure: 'string',
type: SeoFileType.None,
content: 'string',
},
],
},
},
},
},
};

export const mockSEOFilesLoadingState = {
contents: {
...mockInitialState.contents,
seoFiles: {
isLoading: { [contentSEOFilesHash]: true },
error: {},
data: undefined,
isFetched: false,
},
},
};

export const mockSEOFilesErrorState = {
contents: {
...mockInitialState.contents,
seoFiles: {
isLoading: {},
error: { [contentSEOFilesHash]: new Error('Error') as BlackoutError },
data: undefined,
isFetched: true,
},
},
};

export const result = {
error: undefined,
isLoading: false,
isFetched: true,
data: {
entries: [
{
name: 'string',
path: 'string',
uploadDate: '2023-05-23T17:47:02.770Z',
hostId: 0,
subfolderStructure: 'string',
type: SeoFileType.None,
content: 'string',
},
],
number: 1,
totalItems: 1,
totalPages: 1,
},
actions: {
fetch: expect.any(Function),
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ export const mockInitialState = {
metadata: {
error: {},
isLoading: {},
result: null,
result: undefined,
},
seoFiles: {
error: {},
isLoading: {},
result: undefined,
},
searchResults: {},
contentTypes: {
Expand Down
Loading