From c1d16193d90142ce67ea99881349738ab02652b3 Mon Sep 17 00:00:00 2001 From: Filip Drozd Date: Wed, 5 Apr 2023 14:29:19 +0200 Subject: [PATCH] feat: add a skeleton of new data feeds endpoint --- .../sdk-platform/src/endpoints/DataFeeds.ts | 143 ++++++++++++++++++ packages/sdk-platform/src/endpoints/index.ts | 2 + .../sdk-platform/src/interfaces/DataFeeds.ts | 66 ++++++++ packages/sdk-platform/src/makeClient.ts | 2 + packages/sdk-platform/src/routes.ts | 2 + 5 files changed, 215 insertions(+) create mode 100644 packages/sdk-platform/src/endpoints/DataFeeds.ts create mode 100644 packages/sdk-platform/src/interfaces/DataFeeds.ts diff --git a/packages/sdk-platform/src/endpoints/DataFeeds.ts b/packages/sdk-platform/src/endpoints/DataFeeds.ts new file mode 100644 index 00000000..7c864cf6 --- /dev/null +++ b/packages/sdk-platform/src/endpoints/DataFeeds.ts @@ -0,0 +1,143 @@ +import { + Http, + squashAndPreparePositionalArguments +} from '@spree/core-api-v2-sdk' +import type { + NoContentResponse, + NoContentResult +} from '@spree/core-api-v2-sdk' +import type { + IDataFeed, + IDataFeedResult, + IDataFeeds, + IDataFeedsResult, + ListOptions, + ShowOptions, + CreateOptions, + UpdateOptions, + RemoveOptions +} from '../interfaces/DataFeeds' +import routes from '../routes' + +export default class DataFeeds extends Http { + /** + * Returns a list of all Data Feeds. + * + * **Required token:** [Bearer token](../pages/tokens.html#bearer-token) + * + * **Success response schema:** [Success schema](../pages/response-schema.html#success-schema) + * + * **Failure response schema:** [Error schema](../pages/response-schema.html#error-schema) + * + * **Example:** + * ```ts + * const response = await client.dataFeeds.list({ + * bearer_token: '7381273269536713689562374856' + * }) + * ``` + */ + public async list(options: ListOptions): Promise { + console.log(options) + const { token, params } = squashAndPreparePositionalArguments([options], []) + + return await this.spreeResponse('get', routes.dataFeeds(), token, params) + } + + /** + * Returns a single Data Feed by its ID. + * + * **Required token:** [Bearer token](../pages/tokens.html#bearer-token) + * + * **Success response schema:** [Success schema](../pages/response-schema.html#success-schema) + * + * **Failure response schema:** [Error schema](../pages/response-schema.html#error-schema) + * + * **Example:** + * ```ts + * const response = await client.dataFeeds.show({ + * bearer_token: '7381273269536713689562374856' + * id: '1' + * }) + * ``` + */ + public async show(options: ShowOptions): Promise { + const { id, token, params } = squashAndPreparePositionalArguments([options], ['id']) + + return await this.spreeResponse('get', routes.dataFeed(id), token, params) + } + + /** + * Creates a new Data Feed and returns its attributes. + * + * **Required token:** [Bearer token](../pages/tokens.html#bearer-token) + * + * **Success response schema:** [Success schema](../pages/response-schema.html#success-schema) + * + * **Failure response schema:** [Error schema](../pages/response-schema.html#error-schema) + * + * **Example:** + * ```ts + * const response = await client.dataFeeds.create({ + * bearer_token: '7381273269536713689562374856', + * data_feed_setting: { + * provider: 'google', + * name: 'Google data feed' + * } + * }) + * ``` + */ + public async create(options: CreateOptions): Promise { + const { token, params } = squashAndPreparePositionalArguments([options], []) + + return await this.spreeResponse('post', routes.dataFeeds(), token, params) + } + + /** + * Update selected Data Feed. + * + * **Required token:** [Bearer token](../pages/tokens.html#bearer-token) + * + * **Success response schema:** [Success schema](../pages/response-schema.html#success-schema) + * + * **Failure response schema:** [Error schema](../pages/response-schema.html#error-schema) + * + * **Example:** + * ```ts + * const response = await client.dataFeeds.update({ + * bearer_token: '7381273269536713689562374856', + * id: '1', + * data_feed_setting: { + * name: 'New feed name' + * } + * }) + * ``` + */ + public async update(options: UpdateOptions): Promise { + const { id, token, params } = squashAndPreparePositionalArguments([options], ['id']) + + return await this.spreeResponse('patch', routes.dataFeed(id), token, params) + } + + /** + * This endpoint removes the specified Data Feed. + * + * **Required token:** [Bearer token](../pages/tokens.html#bearer-token) + * + * **Success response schema:** [Success schema](../pages/response-schema.html#success-schema) + * + * **Failure response schema:** [Error schema](../pages/response-schema.html#error-schema) + * + * **Example:** + * ```ts + * const response = await client.dataFeeds.remove({ + * bearer_token: '7381273269536713689562374856', + * id: '1' + * }) + * ``` + */ + public async remove(options: RemoveOptions): Promise { + const { id, token, params } = squashAndPreparePositionalArguments([options], ['id']) + + return await this.spreeResponse('delete', routes.dataFeed(id), token, params) + } +} diff --git a/packages/sdk-platform/src/endpoints/index.ts b/packages/sdk-platform/src/endpoints/index.ts index bf735848..3dea6be4 100644 --- a/packages/sdk-platform/src/endpoints/index.ts +++ b/packages/sdk-platform/src/endpoints/index.ts @@ -5,6 +5,7 @@ import Classifications from './Classifications' import Countries from './Countries' import Pages from './Pages' import Sections from './Sections' +import DataFeeds from './DataFeeds' import Digitals from './Digitals' import Links from './Links' import Items from './Items' @@ -50,6 +51,7 @@ export { Countries, Pages, Sections, + DataFeeds, Digitals, Links, Items, diff --git a/packages/sdk-platform/src/interfaces/DataFeeds.ts b/packages/sdk-platform/src/interfaces/DataFeeds.ts new file mode 100644 index 00000000..fa2b640f --- /dev/null +++ b/packages/sdk-platform/src/interfaces/DataFeeds.ts @@ -0,0 +1,66 @@ +import type { + JsonApiDocument, + JsonApiListResponse, + JsonApiSingleResponse, + ResultResponse, + WithCommonOptions +} from '@spree/core-api-v2-sdk' + +export interface DataFeedAttr { + spree_store_id: number + name: string + provider: string + uuid: string + enabled: boolean +} + +export interface DataFeed extends JsonApiDocument { + type: string + id: string + attributes: DataFeedAttr +} + +export interface DataFeedParams { + data_feed_setting: { + spree_store_id: number + name: string + provider: string + enabled: boolean + } +} + +export interface IDataFeed extends JsonApiSingleResponse { + data: DataFeed +} + +export interface IDataFeeds extends JsonApiListResponse { + data: DataFeed[] +} + +export interface IDataFeedResult extends ResultResponse {} + +export interface IDataFeedsResult extends ResultResponse {} + +export type ListOptions = WithCommonOptions< + { suggestToken: true; onlyAccountToken: true; suggestQuery: true } +> + +export type ShowOptions = WithCommonOptions< + { suggestToken: true; onlyAccountToken: true; suggestQuery: true }, + { id: string } +> + +export type CreateOptions = WithCommonOptions< + { suggestToken: true; onlyAccountToken: true; suggestQuery: true }, + DataFeedParams +> + +export type UpdateOptions = WithCommonOptions< + { suggestToken: true; onlyAccountToken: true; suggestQuery: true }, + DataFeedParams & { id: string } +> + +export type RemoveOptions = WithCommonOptions< + { suggestToken: true; onlyAccountToken: true }, + { id: string } +> diff --git a/packages/sdk-platform/src/makeClient.ts b/packages/sdk-platform/src/makeClient.ts index debba40e..55e2b94b 100644 --- a/packages/sdk-platform/src/makeClient.ts +++ b/packages/sdk-platform/src/makeClient.ts @@ -8,6 +8,7 @@ import { Countries, Pages, Sections, + DataFeeds, Digitals, Links, Items, @@ -54,6 +55,7 @@ const endpoints = { countries: Countries, pages: Pages, sections: Sections, + dataFeeds: DataFeeds, digitals: Digitals, links: Links, items: Items, diff --git a/packages/sdk-platform/src/routes.ts b/packages/sdk-platform/src/routes.ts index 3ad7992a..3a2de5a4 100644 --- a/packages/sdk-platform/src/routes.ts +++ b/packages/sdk-platform/src/routes.ts @@ -10,6 +10,8 @@ const endpoints = { classificationPath: (id: string) => `${platformPath}/classifications/${encodeURIComponent(id)}`, countriesPath: (): string => `${platformPath}/countries`, countryPath: (id: string) => `${platformPath}/countries/${encodeURIComponent(id)}`, + dataFeeds: () => `${platformPath}/data_feed_settings`, + dataFeed: (id: string) => `${platformPath}/data_feed_settings/${encodeURIComponent(id)}`, pagesPath: (): string => `${platformPath}/cms_pages`, pagePath: (id: string) => `${platformPath}/cms_pages/${encodeURIComponent(id)}`, sectionsPath: (): string => `${platformPath}/cms_sections`,