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

Commit 50655d2

Browse files
feat: subscriptions endpoint (#848)
1 parent dd8bf04 commit 50655d2

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

src/endpoints/subscriptions.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import CRUDExtend from '../extends/crud'
2+
3+
class SubscriptionsEndpoint extends CRUDExtend {
4+
constructor(endpoint) {
5+
super(endpoint)
6+
7+
this.endpoint = 'subscriptions/subscriptions'
8+
}
9+
10+
Create(body) {
11+
return this.request.send(this.endpoint, 'POST', {
12+
...body
13+
})
14+
}
15+
16+
GetInvoices(id) {
17+
return this.request.send(`${this.endpoint}/${id}/invoices`, 'GET')
18+
}
19+
20+
}
21+
22+
export default SubscriptionsEndpoint

src/moltin.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import { ApplicationKeysEndpoint } from './types/application-keys'
5757
import { SubscriptionProductsEndpoint } from './types/subscription-products'
5858
import { SubscriptionPlansEndpoint } from './types/subscription-plans'
5959
import { SubscriptionOfferingsEndpoint } from './types/subscription-offerings'
60+
import { SubscriptionsEndpoint } from './types/subscriptions'
6061

6162
export * from './types/config'
6263
export * from './types/storage'
@@ -124,6 +125,7 @@ export * from './types/application-keys'
124125
export * from './types/subscription-products'
125126
export * from './types/subscription-plans'
126127
export * from './types/subscription-offerings'
128+
export * from './types/subscriptions'
127129

128130
// UMD
129131
export as namespace moltin
@@ -183,6 +185,7 @@ export class Moltin {
183185
SubscriptionProducts: SubscriptionProductsEndpoint
184186
SubscriptionPlans: SubscriptionPlansEndpoint
185187
SubscriptionOfferings: SubscriptionOfferingsEndpoint
188+
Subscriptions: SubscriptionsEndpoint
186189

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

src/moltin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import ApplicationKeysEndpoint from './endpoints/application-keys'
5050
import SubscriptionProductsEndpoint from './endpoints/subscription-products'
5151
import SubscriptionPlansEndpoint from './endpoints/subscription-plan'
5252
import SubscriptionOfferingsEndpoint from './endpoints/subscription-offerings'
53+
import SubscriptionsEndpoint from './endpoints/subscriptions'
5354

5455
import {cartIdentifier, tokenInvalid, getCredentials, resolveCredentialsStorageKey} from './utils/helpers'
5556
import CatalogsEndpoint from './endpoints/catalogs'
@@ -118,6 +119,7 @@ export default class Moltin {
118119
this.SubscriptionProducts = new SubscriptionProductsEndpoint(config)
119120
this.SubscriptionPlans = new SubscriptionPlansEndpoint(config)
120121
this.SubscriptionOfferings = new SubscriptionOfferingsEndpoint(config)
122+
this.Subscriptions = new SubscriptionsEndpoint(config)
121123
}
122124

123125
// Expose `Cart` class on Moltin class

src/types/subscriptions.d.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* Subscriptions
3+
* Description: Subscriptions.
4+
* DOCS: TODO: add docs when ready
5+
*/
6+
import {
7+
Identifiable,
8+
CrudQueryableResource,
9+
Resource
10+
} from './core'
11+
12+
/**
13+
* Core Subscription Base Interface
14+
* For custom flows, extend this interface
15+
* DOCS: TODO: add docs when ready
16+
*/
17+
export interface SubscriptionBase {
18+
type: "subscription",
19+
attributes: {
20+
account_id: string,
21+
offering: {
22+
id: string
23+
type: "subscription-offering",
24+
attributes: {
25+
name: string,
26+
description: string,
27+
updated_at: string,
28+
created_at: string
29+
},
30+
relationships: {
31+
plans: {
32+
links: {
33+
related: string,
34+
self: string
35+
},
36+
data: {
37+
type: "offering-plan",
38+
id: string
39+
}
40+
}
41+
},
42+
meta: {
43+
owner: string
44+
}
45+
},
46+
plan_id: string,
47+
currency: string,
48+
updated_at: string,
49+
created_at: string
50+
},
51+
meta: {
52+
owner: string
53+
}
54+
}
55+
56+
export interface SubscriptionCreate {
57+
account_id: string,
58+
offering_id: string,
59+
plan_id: string,
60+
currency: string,
61+
meta?: {
62+
owner?: string
63+
}
64+
}
65+
66+
export interface SubscriptionInvoice extends Identifiable {
67+
type: "subscription-invoice",
68+
attributes: {
69+
billing_period: {
70+
start: string,
71+
end: string
72+
},
73+
invoice_items: {
74+
description: string,
75+
price: {
76+
currency: string,
77+
amount: number,
78+
includes_tax: boolean
79+
}
80+
}[],
81+
outstanding: boolean,
82+
updated_at: string,
83+
created_at: string
84+
},
85+
meta: {
86+
owner: string,
87+
subscription_id: string,
88+
price: {
89+
currency: string,
90+
amount: number,
91+
includes_tax: boolean
92+
}
93+
}
94+
}
95+
96+
97+
export interface Subscription extends Identifiable, SubscriptionBase {
98+
99+
}
100+
101+
/**
102+
* Subscription Endpoints
103+
* DOCS: TODO: add docs when ready
104+
*/
105+
export interface SubscriptionsEndpoint
106+
extends Omit<CrudQueryableResource<
107+
Subscription,
108+
SubscriptionCreate,
109+
never,
110+
never,
111+
never,
112+
never
113+
>, "Filter" | "Limit" | "Offset" | "Sort" | "With" | "Attributes" | "Update" | "Link" > {
114+
endpoint: 'subscriptions'
115+
116+
GetInvoices(id: string): Promise<Resource<SubscriptionInvoice[]>>
117+
}

0 commit comments

Comments
 (0)