Skip to content
Merged
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
62 changes: 62 additions & 0 deletions src/endpoints/custom-relationships.js
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
107 changes: 107 additions & 0 deletions src/types/custom-relationships.d.ts
Original file line number Diff line number Diff line change
@@ -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<CustomRelationshipBase, 'attributes'> {}

export interface UpdateCustomRelationshipBody
extends Identifiable,
Pick<CustomRelationshipBase, 'attributes'> {}

export interface CustomRelationshipsFilter {
eq?: {
owner?: 'organization' | 'store'
}
}

export interface CustomRelationshipsListResponse
extends ResourceList<CustomRelationship> {
links?: { [key: string]: string | null } | {}
meta: {
results: {
total: number
}
}
}

export interface CustomRelationshipsEndpoint {
endpoint: 'custom_relationships'
/**
* List Custom Relationships
*/
All(): Promise<CustomRelationshipsListResponse>

/**
* Get Custom Relationship
* @param slug - The slug of the Custom Relationship. It should always be prefixed with 'CRP_'
*/
Get(slug: string): Promise<Resource<CustomRelationship>>

/**
* Create Custom Relationship
* @param body - The base attributes of the Custom Relationships
*/
Create(
body: CreateCustomRelationshipBody
): Promise<Resource<CustomRelationship>>

/**
* 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<Resource<CustomRelationship>>

/**
* 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
}