Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
feat(custom apis): adds the custom apis endpoint (#929)
Browse files Browse the repository at this point in the history
* feat(custom apis): adds the custom apis endpoint

* updated export

* updated validation

* updated validation and its fields to optional

* updated name for validation

* updated to use slug
  • Loading branch information
sarawasim1 authored May 31, 2024
1 parent d584d96 commit 39135f4
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 0 deletions.
117 changes: 117 additions & 0 deletions src/endpoints/custom-apis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import CRUDExtend from '../extends/crud'

import { buildURL } from '../utils/helpers'

class CustomApisEndpoint extends CRUDExtend {
constructor(endpoint) {
super(endpoint)

this.endpoint = 'settings/extensions/custom-apis'
this.entriesEndpoint = 'extensions'
}

Create(body) {
return this.request.send(
this.endpoint,
'POST',
body
)
}

Update(id, body) {
return this.request.send(
`${this.endpoint}/${id}`,
'PUT',
body
)
}

GetFields(customApiId) {
const { limit, offset, sort } = this

return this.request.send(
buildURL(`${this.endpoint}/${customApiId}/fields`, {
limit,
offset,
sort
}),
'GET'
)
}

GetField(customApiId, customApiFieldId) {
return this.request.send(
`${this.endpoint}/${customApiId}/fields/${customApiFieldId}`,
'GET'
)
}

CreateField(customApiId, body) {
return this.request.send(
`${this.endpoint}/${customApiId}/fields`,
'POST',
body
)
}

UpdateField(customApiId, customApiFieldId, body) {
return this.request.send(
`${this.endpoint}/${customApiId}/fields/${customApiFieldId}`,
'PUT',
body
)
}

DeleteField(customApiId, customApiFieldId) {
return this.request.send(
`${this.endpoint}/${customApiId}/fields/${customApiFieldId}`,
'DELETE'
)
}

GetEntries(customApiSlug) {
const { limit, offset, sort, filter } = this

return this.request.send(
buildURL(`${this.entriesEndpoint}/${customApiSlug}`, {
limit,
offset,
sort,
filter
}),
'GET'
)
}

GetEntry(customApiSlug, customApiEntryId) {
return this.request.send(
`${this.entriesEndpoint}/${customApiSlug}/${customApiEntryId}`,
'GET'
)
}

CreateEntry(customApiSlug, body) {
return this.request.send(
`${this.entriesEndpoint}/${customApiSlug}`,
'POST',
body
)
}

UpdateEntry(customApiSlug, customApiEntryId, body) {
return this.request.send(
`${this.entriesEndpoint}/${customApiSlug}/${customApiEntryId}`,
'PUT',
body
)
}

DeleteEntry(customApiSlug, customApiEntryId) {
return this.request.send(
`${this.entriesEndpoint}/${customApiSlug}/${customApiEntryId}`,
'DELETE'
)
}
}

export default CustomApisEndpoint
3 changes: 3 additions & 0 deletions src/moltin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import { RulePromotionsEndpoint } from './types/rule-promotions'
import { SubscriptionSubscribersEndpoint } from './types/subscription-subscribers'
import { SubscriptionJobsEndpoint } from './types/subscription-jobs'
import { SubscriptionSchedulesEndpoint } from './types/subscription-schedules'
import { CustomApisEndpoint } from './types/custom-apis'

export * from './types/config'
export * from './types/storage'
Expand Down Expand Up @@ -138,6 +139,7 @@ export * from './types/rule-promotions'
export * from './types/subscription-subscribers'
export * from './types/subscription-jobs'
export * from './types/subscription-schedules'
export * from './types/custom-apis'

// UMD
export as namespace moltin
Expand Down Expand Up @@ -203,6 +205,7 @@ export class Moltin {
SubscriptionSubscribers : SubscriptionSubscribersEndpoint
SubscriptionJobs : SubscriptionJobsEndpoint
SubscriptionSchedules: SubscriptionSchedulesEndpoint
CustomApis: CustomApisEndpoint

Cart(id?: string): CartEndpoint // This optional cart id is super worrying when using the SDK in a node server :/
constructor(config: Config)
Expand Down
2 changes: 2 additions & 0 deletions src/moltin.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import SubscriptionSchedulesEndpoint from './endpoints/subscription-schedules'
import {cartIdentifier, tokenInvalid, getCredentials, resolveCredentialsStorageKey} from './utils/helpers'
import CatalogsEndpoint from './endpoints/catalogs'
import ShopperCatalogEndpoint from './endpoints/catalog'
import CustomApisEndpoint from './endpoints/custom-apis'

export default class Moltin {
constructor(config) {
Expand Down Expand Up @@ -130,6 +131,7 @@ export default class Moltin {
this.SubscriptionSubscribers = new SubscriptionSubscribersEndpoint(config)
this.SubscriptionJobs = new SubscriptionJobsEndpoint(config)
this.SubscriptionSchedules = new SubscriptionSchedulesEndpoint(config)
this.CustomApis = new CustomApisEndpoint(config)
}

// Expose `Cart` class on Moltin class
Expand Down
117 changes: 117 additions & 0 deletions src/types/custom-apis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* Commerce Extensions
* Description: Commerce Extensions allows for the creation of Custom APIs that can manage large, private data sets efficiently, offering both simple and complex multidimensional filtering options.
* DOCS: https://elasticpath.dev/docs/commerce-cloud/commerce-extensions/overview
*/

import { Identifiable, Resource, ResourcePage } from './core';


export interface CustomApiBase {
name: string
description: string
api_type: string
type: string
slug: string
}

export interface CustomApi extends Identifiable, CustomApiBase {
links: {
self: string
}
meta: {
timestamps: {
created_at: string
updated_at: string
}
}
}

export type CustomFieldValidation =
| { string: { min_length?: number, max_length?: number, regex?: string, allow_null_values?: boolean } }
| { integer: { min_value?: number, max_value?: number, allow_null_values?: boolean } }
| { float: { min_value?: number, max_value?: number, allow_null_values?: boolean } }
| { boolean: { allow_null_values?: boolean } }

export interface CustomApiFieldBase {
name: string
description: string
field_type: string
type: string
slug: string
validation?: CustomFieldValidation
}

export interface CustomApiField extends Identifiable, CustomApiFieldBase {
links: {
self: string
}
meta: {
timestamps: {
created_at: string
updated_at: string
}
}
}

export interface CustomApisEndpoint {
endpoint: 'settings/extensions/custom-apis'
entriesEndpoint: 'extensions'

All(token?: string): Promise<ResourcePage<CustomApi>>

Get(id: string, token?: string): Promise<Resource<CustomApi>>

Filter(filter: any): CustomApisEndpoint

Limit(value: number): CustomApisEndpoint

Offset(value: number): CustomApisEndpoint

Sort(value: string): CustomApisEndpoint

Create(body: CustomApiBase): Promise<Resource<CustomApi>>

Update(
id: string,
body: Partial<CustomApiBase>,
token?: string
): Promise<Resource<CustomApi>>

Delete(id: string): Promise<{}>

GetFields<T = any>(customApiId: string): Promise<T>

GetField<T = any>(customApiId: string, customApiFieldId:string): Promise<T>

CreateField<RequestBody = CustomApiFieldBase, ResponseBody = CustomApiField>(
customApiId: string,
body: RequestBody
): Promise<ResponseBody>

UpdateField<RequestBody = CustomApiFieldBase, ResponseBody = CustomApiField>(
customApiId: string,
customApiFieldId: string,
body: RequestBody
): Promise<ResponseBody>

DeleteField<T = any>(customApiId: string, customApiFieldId: string): Promise<T>

GetEntries<T = any>(customApiSlug: string): Promise<T>

GetEntry<T = any>(customApiSlug: string, customApiEntryId: string): Promise<T>

CreateEntry<RequestBody = any, ResponseBody = any>(
customApiSlug: string,
body: RequestBody
): Promise<ResponseBody>

UpdateEntry<RequestBody = any, ResponseBody = any>(
customApiSlug: string,
customApiEntryId: string,
body: RequestBody
): Promise<ResponseBody>

DeleteEntry<T = any>(customApiSlug: string, customApiEntryId: string): Promise<T>

}

0 comments on commit 39135f4

Please sign in to comment.