Skip to content

Commit 6a49c34

Browse files
feat: Add custom relationships endpoint and type (#8)
BREAKING CHANGE: Added a new endpoint implementation and types
1 parent 0a1d4a8 commit 6a49c34

File tree

4 files changed

+174
-0
lines changed

4 files changed

+174
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import RequestFactory from '../factories/request'
2+
import { buildURL } from '../utils/helpers'
3+
4+
class CustomRelationshipsEndpoint {
5+
constructor(endpoint) {
6+
const config = { ...endpoint }
7+
config.version = 'pcm'
8+
this.request = new RequestFactory(config)
9+
this.endpoint = 'custom_relationships'
10+
}
11+
12+
All() {
13+
const { filter, limit, offset } = this
14+
return this.request.send(
15+
buildURL(this.endpoint, {
16+
filter,
17+
limit,
18+
offset
19+
}),
20+
'GET'
21+
)
22+
}
23+
24+
Get(slug) {
25+
return this.request.send(`${this.endpoint}/${slug}`, 'GET')
26+
}
27+
28+
Create(body) {
29+
return this.request.send(this.endpoint, 'POST', {
30+
...body,
31+
type: 'custom-relationship'
32+
})
33+
}
34+
35+
Update(slug, body) {
36+
return this.request.send(`${this.endpoint}/${slug}`, 'PUT', {
37+
...body,
38+
type: 'custom-relationship'
39+
})
40+
}
41+
42+
Delete(slug) {
43+
return this.request.send(`${this.endpoint}/${slug}`, 'DELETE')
44+
}
45+
46+
Filter(filter) {
47+
this.filter = filter
48+
return this
49+
}
50+
51+
Limit(value) {
52+
this.limit = value
53+
return this
54+
}
55+
56+
Offset(value) {
57+
this.offset = value
58+
return this
59+
}
60+
}
61+
62+
export default CustomRelationshipsEndpoint

src/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import { CustomApisEndpoint } from './types/custom-apis'
6868
import { SubscriptionDunningRulesEndpoint } from './types/subscription-dunning-rules'
6969
import { SubscriptionProrationPoliciesEndpoint } from './types/subscription-proration-policies'
7070
import { SubscriptionInvoicesEndpoint } from './types/subscription-invoices'
71+
import { CustomRelationshipsEndpoint } from './types/custom-relationships'
7172

7273
export * from './types/config'
7374
export * from './types/storage'
@@ -146,6 +147,7 @@ export * from './types/custom-apis'
146147
export * from './types/subscription-dunning-rules'
147148
export * from './types/subscription-proration-policies'
148149
export * from './types/subscription-invoices'
150+
export * from './types/custom-relationships'
149151

150152
// UMD
151153
export as namespace elasticpath
@@ -215,6 +217,7 @@ export class ElasticPath {
215217
SubscriptionDunningRules: SubscriptionDunningRulesEndpoint
216218
SubscriptionProrationPolicies: SubscriptionProrationPoliciesEndpoint
217219
SubscriptionInvoices: SubscriptionInvoicesEndpoint
220+
CustomRelationships: CustomRelationshipsEndpoint
218221

219222
Cart(id?: string): CartEndpoint // This optional cart id is super worrying when using the SDK in a node server :/
220223
constructor(config: Config)

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import SubscriptionSchedulesEndpoint from './endpoints/subscription-schedules'
5959
import SubscriptionDunningRulesEndpoint from './endpoints/subscription-dunning-rules'
6060
import SubscriptionProrationPoliciesEndpoint from './endpoints/subscription-proration-policies'
6161
import SubscriptionInvoicesEndpoint from './endpoints/subscription-invoices'
62+
import CustomRelationshipsEndpoint from './endpoints/custom-relationships'
6263

6364
import {
6465
cartIdentifier,
@@ -148,6 +149,7 @@ export default class ElasticPath {
148149
this.SubscriptionProrationPolicies =
149150
new SubscriptionProrationPoliciesEndpoint(config)
150151
this.SubscriptionInvoices = new SubscriptionInvoicesEndpoint(config)
152+
this.CustomRelationships = new CustomRelationshipsEndpoint(config)
151153
}
152154

153155
// Expose `Cart` class on ElasticPath class
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* Custom Relationships
3+
* Description: Custom Relationships
4+
*/
5+
6+
import {
7+
Identifiable,
8+
CrudQueryableResource,
9+
ResourcePage,
10+
ResourceList,
11+
Resource
12+
} from './core'
13+
14+
export interface CustomRelationshipBaseAttributes {
15+
name: string
16+
description?: string
17+
slug: string
18+
}
19+
20+
export interface CustomRelationshipBase {
21+
type: 'custom-relationship'
22+
attributes: CustomRelationshipBaseAttributes
23+
meta: {
24+
owner: 'organization' | 'store'
25+
timestamps: {
26+
created_at: string
27+
updated_at: string
28+
}
29+
}
30+
}
31+
32+
export interface CustomRelationship
33+
extends Identifiable,
34+
CustomRelationshipBase {}
35+
36+
export interface CreateCustomRelationshipBody
37+
extends Pick<CustomRelationshipBase, 'attributes'> {}
38+
39+
export interface UpdateCustomRelationshipBody
40+
extends Identifiable,
41+
Pick<CustomRelationshipBase, 'attributes'> {}
42+
43+
export interface CustomRelationshipsFilter {
44+
eq?: {
45+
owner?: 'organization' | 'store'
46+
}
47+
}
48+
49+
export interface CustomRelationshipsListResponse
50+
extends ResourceList<CustomRelationship> {
51+
links?: { [key: string]: string | null } | {}
52+
meta: {
53+
results: {
54+
total: number
55+
}
56+
}
57+
}
58+
59+
export interface CustomRelationshipsEndpoint {
60+
endpoint: 'custom_relationships'
61+
/**
62+
* List Custom Relationships
63+
*/
64+
All(): Promise<CustomRelationshipsListResponse>
65+
66+
/**
67+
* Get Custom Relationship
68+
* @param slug - The slug of the Custom Relationship. It should always be prefixed with 'CRP_'
69+
*/
70+
Get(slug: string): Promise<Resource<CustomRelationship>>
71+
72+
/**
73+
* Create Custom Relationship
74+
* @param body - The base attributes of the Custom Relationships
75+
*/
76+
Create(
77+
body: CreateCustomRelationshipBody
78+
): Promise<Resource<CustomRelationship>>
79+
80+
/**
81+
* Update Custom Relationship
82+
* @param slug - The slug of the Custom Relationship. It should always be prefixed with 'CRP_'
83+
* @param body - The base attributes of the Custom Relationships.
84+
* The Slug attribute cannot be updated and the slug within this object should match this function's first argument.
85+
*/
86+
Update(
87+
slug: string,
88+
body: UpdateCustomRelationshipBody
89+
): Promise<Resource<CustomRelationship>>
90+
91+
/**
92+
* Delete Custom Relationship
93+
* @param slug - The slug of the Custom Relationship. It should always be prefixed with 'CRP_'
94+
*/
95+
Delete(slug: string): Promise<{}>
96+
97+
/**
98+
* Custom Relationship filtering
99+
* @param filter - The filter object.
100+
* Currently supports the 'eq' filter operator for the 'owner' field of the Custom Relationship.
101+
*/
102+
Filter(filter: CustomRelationshipsFilter): CustomRelationshipsEndpoint
103+
104+
Limit(value: number): CustomRelationshipsEndpoint
105+
106+
Offset(value: number): CustomRelationshipsEndpoint
107+
}

0 commit comments

Comments
 (0)