From 77fa4d146b2fde8b1bffdb37146ff0217490c588 Mon Sep 17 00:00:00 2001 From: George Botzakis Date: Fri, 2 Aug 2024 18:18:56 +0100 Subject: [PATCH] feat: Added custom relationships endpoint and type --- src/endpoints/custom-relationships.js | 62 +++++++++++++++ src/index.d.ts | 3 + src/index.js | 2 + src/types/custom-relationships.d.ts | 107 ++++++++++++++++++++++++++ 4 files changed, 174 insertions(+) create mode 100644 src/endpoints/custom-relationships.js create mode 100644 src/types/custom-relationships.d.ts diff --git a/src/endpoints/custom-relationships.js b/src/endpoints/custom-relationships.js new file mode 100644 index 0000000..44f0907 --- /dev/null +++ b/src/endpoints/custom-relationships.js @@ -0,0 +1,62 @@ +import RequestFactory from '../factories/request' +import { buildURL } from '../utils/helpers' + +class CustomRelationshipsEndpoint { + constructor(endpoint) { + const config = { ...endpoint } + config.version = 'pcm' + this.request = new RequestFactory(config) + this.endpoint = 'custom_relationships' + } + + All() { + const { filter, limit, offset } = this + return this.request.send( + buildURL(this.endpoint, { + filter, + limit, + offset + }), + 'GET' + ) + } + + Get(slug) { + return this.request.send(`${this.endpoint}/${slug}`, 'GET') + } + + Create(body) { + return this.request.send(this.endpoint, 'POST', { + ...body, + type: 'custom-relationship' + }) + } + + Update(slug, body) { + return this.request.send(`${this.endpoint}/${slug}`, 'PUT', { + ...body, + type: 'custom-relationship' + }) + } + + Delete(slug) { + return this.request.send(`${this.endpoint}/${slug}`, 'DELETE') + } + + Filter(filter) { + this.filter = filter + return this + } + + Limit(value) { + this.limit = value + return this + } + + Offset(value) { + this.offset = value + return this + } +} + +export default CustomRelationshipsEndpoint diff --git a/src/index.d.ts b/src/index.d.ts index 37f4b8d..a3a55e0 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -68,6 +68,7 @@ import { CustomApisEndpoint } from './types/custom-apis' import { SubscriptionDunningRulesEndpoint } from './types/subscription-dunning-rules' import { SubscriptionProrationPoliciesEndpoint } from './types/subscription-proration-policies' import { SubscriptionInvoicesEndpoint } from './types/subscription-invoices' +import { CustomRelationshipsEndpoint } from './types/custom-relationships' export * from './types/config' export * from './types/storage' @@ -146,6 +147,7 @@ export * from './types/custom-apis' export * from './types/subscription-dunning-rules' export * from './types/subscription-proration-policies' export * from './types/subscription-invoices' +export * from './types/custom-relationships' // UMD export as namespace elasticpath @@ -215,6 +217,7 @@ export class ElasticPath { SubscriptionDunningRules: SubscriptionDunningRulesEndpoint SubscriptionProrationPolicies: SubscriptionProrationPoliciesEndpoint SubscriptionInvoices: SubscriptionInvoicesEndpoint + CustomRelationships: CustomRelationshipsEndpoint Cart(id?: string): CartEndpoint // This optional cart id is super worrying when using the SDK in a node server :/ constructor(config: Config) diff --git a/src/index.js b/src/index.js index 4bb33c2..163acbd 100644 --- a/src/index.js +++ b/src/index.js @@ -59,6 +59,7 @@ import SubscriptionSchedulesEndpoint from './endpoints/subscription-schedules' import SubscriptionDunningRulesEndpoint from './endpoints/subscription-dunning-rules' import SubscriptionProrationPoliciesEndpoint from './endpoints/subscription-proration-policies' import SubscriptionInvoicesEndpoint from './endpoints/subscription-invoices' +import CustomRelationshipsEndpoint from './endpoints/custom-relationships' import { cartIdentifier, @@ -148,6 +149,7 @@ export default class ElasticPath { this.SubscriptionProrationPolicies = new SubscriptionProrationPoliciesEndpoint(config) this.SubscriptionInvoices = new SubscriptionInvoicesEndpoint(config) + this.CustomRelationships = new CustomRelationshipsEndpoint(config) } // Expose `Cart` class on ElasticPath class diff --git a/src/types/custom-relationships.d.ts b/src/types/custom-relationships.d.ts new file mode 100644 index 0000000..c0c981e --- /dev/null +++ b/src/types/custom-relationships.d.ts @@ -0,0 +1,107 @@ +/** + * Custom Relationships + * Description: Custom Relationships + */ + +import { + Identifiable, + CrudQueryableResource, + ResourcePage, + ResourceList, + Resource +} from './core' + +export interface CustomRelationshipBaseAttributes { + name: string + description?: string + slug: string +} + +export interface CustomRelationshipBase { + type: 'custom-relationship' + attributes: CustomRelationshipBaseAttributes + meta: { + owner: 'organization' | 'store' + timestamps: { + created_at: string + updated_at: string + } + } +} + +export interface CustomRelationship + extends Identifiable, + CustomRelationshipBase {} + +export interface CreateCustomRelationshipBody + extends Pick {} + +export interface UpdateCustomRelationshipBody + extends Identifiable, + Pick {} + +export interface CustomRelationshipsFilter { + eq?: { + owner?: 'organization' | 'store' + } +} + +export interface CustomRelationshipsListResponse + extends ResourceList { + links?: { [key: string]: string | null } | {} + meta: { + results: { + total: number + } + } +} + +export interface CustomRelationshipsEndpoint { + endpoint: 'custom_relationships' + /** + * List Custom Relationships + */ + All(): Promise + + /** + * Get Custom Relationship + * @param slug - The slug of the Custom Relationship. It should always be prefixed with 'CRP_' + */ + Get(slug: string): Promise> + + /** + * Create Custom Relationship + * @param body - The base attributes of the Custom Relationships + */ + Create( + body: CreateCustomRelationshipBody + ): Promise> + + /** + * Update Custom Relationship + * @param slug - The slug of the Custom Relationship. It should always be prefixed with 'CRP_' + * @param body - The base attributes of the Custom Relationships. + * The Slug attribute cannot be updated and the slug within this object should match this function's first argument. + */ + Update( + slug: string, + body: UpdateCustomRelationshipBody + ): Promise> + + /** + * Delete Custom Relationship + * @param slug - The slug of the Custom Relationship. It should always be prefixed with 'CRP_' + */ + Delete(slug: string): Promise<{}> + + /** + * Custom Relationship filtering + * @param filter - The filter object. + * Currently supports the 'eq' filter operator for the 'owner' field of the Custom Relationship. + */ + Filter(filter: CustomRelationshipsFilter): CustomRelationshipsEndpoint + + Limit(value: number): CustomRelationshipsEndpoint + + Offset(value: number): CustomRelationshipsEndpoint +}