@shoplygg/node 0.1.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -0
- package/generated/api.d.ts +1304 -47
- package/generated/operations.js +32 -3
- package/index.d.ts +43 -1
- package/index.js +134 -4
- package/package.json +2 -2
package/generated/operations.js
CHANGED
|
@@ -173,14 +173,31 @@ export const OPERATIONS = [
|
|
|
173
173
|
{"id":"cartRecovery.update","name":"cartRecovery.update","method":"PUT","path":"/stores/{storeId}/cart-recovery","params":[],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
174
174
|
{"id":"cartRecovery.carts","name":"cartRecovery.carts","method":"GET","path":"/stores/{storeId}/cart-recovery/carts","params":[],"query":["range","from","to","tz","show","search","page"],"body":false,"upload":null,"file":false,"paged":true,"customer":false,"signedIn":true,"idempotent":false},
|
|
175
175
|
{"id":"cartRecovery.cart","name":"cartRecovery.cart","method":"GET","path":"/stores/{storeId}/cart-recovery/carts/{cartId}","params":["cartId"],"query":[],"body":false,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":false},
|
|
176
|
-
{"id":"
|
|
176
|
+
{"id":"winBack.show","name":"winBack.show","method":"GET","path":"/stores/{storeId}/win-back","params":[],"query":["range","from","to","tz"],"body":false,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":false},
|
|
177
|
+
{"id":"winBack.update","name":"winBack.update","method":"PUT","path":"/stores/{storeId}/win-back","params":[],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
178
|
+
{"id":"winBack.buyers","name":"winBack.buyers","method":"GET","path":"/stores/{storeId}/win-back/buyers","params":[],"query":["show","search","page"],"body":false,"upload":null,"file":false,"paged":true,"customer":false,"signedIn":true,"idempotent":false},
|
|
179
|
+
{"id":"winBack.buyer","name":"winBack.buyer","method":"GET","path":"/stores/{storeId}/win-back/buyers/{buyerId}","params":["buyerId"],"query":[],"body":false,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":false},
|
|
180
|
+
{"id":"subscribers.summary","name":"subscribers.summary","method":"GET","path":"/stores/{storeId}/subscribers/summary","params":[],"query":["range","from","to","tz"],"body":false,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":false},
|
|
181
|
+
{"id":"subscribers.asking","name":"subscribers.asking","method":"PUT","path":"/stores/{storeId}/subscribers/asking","params":[],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
182
|
+
{"id":"subscribers.list","name":"subscribers.list","method":"GET","path":"/stores/{storeId}/subscribers","params":[],"query":["show","search","page"],"body":false,"upload":null,"file":false,"paged":true,"customer":false,"signedIn":true,"idempotent":false},
|
|
183
|
+
{"id":"subscribers.export","name":"subscribers.export","method":"GET","path":"/stores/{storeId}/subscribers/export","params":[],"query":["show","search","check"],"body":false,"upload":null,"file":true,"paged":false,"customer":false,"signedIn":true,"idempotent":false},
|
|
184
|
+
{"id":"subscribers.unsubscribe","name":"subscribers.unsubscribe","method":"POST","path":"/stores/{storeId}/subscribers/unsubscribe","params":[],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
185
|
+
{"id":"customers.list","name":"customers.list","method":"GET","path":"/stores/{storeId}/customers","params":[],"query":["page","perPage","search","sort","only","tag","range","from","to","tz"],"body":false,"upload":null,"file":false,"paged":true,"customer":false,"signedIn":true,"idempotent":false},
|
|
177
186
|
{"id":"customers.create","name":"customers.create","method":"POST","path":"/stores/{storeId}/customers","params":[],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
178
|
-
{"id":"customers.export","name":"customers.export","method":"GET","path":"/stores/{storeId}/customers/export","params":[],"query":["search","sort","only","check","range","from","to","tz"],"body":false,"upload":null,"file":true,"paged":false,"customer":false,"signedIn":true,"idempotent":false},
|
|
187
|
+
{"id":"customers.export","name":"customers.export","method":"GET","path":"/stores/{storeId}/customers/export","params":[],"query":["search","sort","only","tag","check","range","from","to","tz"],"body":false,"upload":null,"file":true,"paged":false,"customer":false,"signedIn":true,"idempotent":false},
|
|
179
188
|
{"id":"customers.stats","name":"customers.stats","method":"GET","path":"/stores/{storeId}/customers/stats","params":[],"query":["range","from","to","tz"],"body":false,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":false},
|
|
180
189
|
{"id":"customers.show","name":"customers.show","method":"GET","path":"/stores/{storeId}/customers/{customerId}","params":["customerId"],"query":[],"body":false,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":false},
|
|
181
190
|
{"id":"customers.setBalance","name":"customers.setBalance","method":"POST","path":"/stores/{storeId}/customers/{customerId}/balance","params":["customerId"],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
182
191
|
{"id":"customers.blacklist","name":"customers.blacklist","method":"POST","path":"/stores/{storeId}/customers/{customerId}/blacklist","params":["customerId"],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
183
192
|
{"id":"customers.unblacklist","name":"customers.unblacklist","method":"DELETE","path":"/stores/{storeId}/customers/{customerId}/blacklist","params":["customerId"],"query":["addresses"],"body":false,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
193
|
+
{"id":"customerTags.list","name":"customerTags.list","method":"GET","path":"/stores/{storeId}/customer-tags","params":[],"query":[],"body":false,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":false},
|
|
194
|
+
{"id":"customerTags.create","name":"customerTags.create","method":"POST","path":"/stores/{storeId}/customer-tags","params":[],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
195
|
+
{"id":"customerTags.automatic","name":"customerTags.automatic","method":"PUT","path":"/stores/{storeId}/customer-tags/automatic","params":[],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
196
|
+
{"id":"customerTags.update","name":"customerTags.update","method":"PUT","path":"/stores/{storeId}/customer-tags/{tagId}","params":["tagId"],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
197
|
+
{"id":"customerTags.delete","name":"customerTags.delete","method":"DELETE","path":"/stores/{storeId}/customer-tags/{tagId}","params":["tagId"],"query":[],"body":false,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
198
|
+
{"id":"customers.setTags","name":"customers.setTags","method":"PUT","path":"/stores/{storeId}/customers/{customerId}/tags","params":["customerId"],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
199
|
+
{"id":"customers.tag","name":"customers.tag","method":"PUT","path":"/stores/{storeId}/customers/{customerId}/tags/{tagId}","params":["customerId","tagId"],"query":[],"body":false,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
200
|
+
{"id":"customers.untag","name":"customers.untag","method":"DELETE","path":"/stores/{storeId}/customers/{customerId}/tags/{tagId}","params":["customerId","tagId"],"query":[],"body":false,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
184
201
|
{"id":"affiliates.list","name":"affiliates.list","method":"GET","path":"/stores/{storeId}/affiliates","params":[],"query":[],"body":false,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":false},
|
|
185
202
|
{"id":"affiliates.create","name":"affiliates.create","method":"POST","path":"/stores/{storeId}/affiliates","params":[],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
186
203
|
{"id":"affiliates.update","name":"affiliates.update","method":"PUT","path":"/stores/{storeId}/affiliates/{affiliateId}","params":["affiliateId"],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
@@ -195,6 +212,13 @@ export const OPERATIONS = [
|
|
|
195
212
|
{"id":"affiliates.toStoreBalance","name":"affiliates.toStoreBalance","method":"POST","path":"/stores/{storeId}/affiliates/{affiliateId}/to-store-balance","params":["affiliateId"],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
196
213
|
{"id":"affiliateProgramme.show","name":"affiliateProgramme.show","method":"GET","path":"/stores/{storeId}/affiliate-programme","params":[],"query":[],"body":false,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":false},
|
|
197
214
|
{"id":"affiliateProgramme.update","name":"affiliateProgramme.update","method":"PUT","path":"/stores/{storeId}/affiliate-programme","params":[],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
215
|
+
{"id":"tickets.list","name":"tickets.list","method":"GET","path":"/stores/{storeId}/support-tickets","params":[],"query":["show","search","page","perPage"],"body":false,"upload":null,"file":false,"paged":true,"customer":false,"signedIn":true,"idempotent":false},
|
|
216
|
+
{"id":"tickets.settings","name":"tickets.settings","method":"PUT","path":"/stores/{storeId}/support-tickets/settings","params":[],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
217
|
+
{"id":"tickets.get","name":"tickets.get","method":"GET","path":"/stores/{storeId}/support-tickets/{ticketId}","params":["ticketId"],"query":[],"body":false,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":false},
|
|
218
|
+
{"id":"tickets.reply","name":"tickets.reply","method":"POST","path":"/stores/{storeId}/support-tickets/{ticketId}/reply","params":["ticketId"],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
219
|
+
{"id":"tickets.resolve","name":"tickets.resolve","method":"POST","path":"/stores/{storeId}/support-tickets/{ticketId}/resolve","params":["ticketId"],"query":[],"body":false,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
220
|
+
{"id":"tickets.reopen","name":"tickets.reopen","method":"POST","path":"/stores/{storeId}/support-tickets/{ticketId}/reopen","params":["ticketId"],"query":[],"body":false,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
221
|
+
{"id":"tickets.picture","name":"tickets.picture","method":"GET","path":"/stores/{storeId}/support-tickets/{ticketId}/pictures/{name}","params":["ticketId","name"],"query":[],"body":false,"upload":null,"file":true,"paged":false,"customer":false,"signedIn":true,"idempotent":false},
|
|
198
222
|
{"id":"blacklist.list","name":"blacklist.list","method":"GET","path":"/stores/{storeId}/blacklist","params":[],"query":["page","perPage","status","type","search"],"body":false,"upload":null,"file":false,"paged":true,"customer":false,"signedIn":true,"idempotent":false},
|
|
199
223
|
{"id":"blacklist.create","name":"blacklist.create","method":"POST","path":"/stores/{storeId}/blacklist","params":[],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":false,"signedIn":true,"idempotent":true},
|
|
200
224
|
{"id":"blacklist.export","name":"blacklist.export","method":"GET","path":"/stores/{storeId}/blacklist/export","params":[],"query":["status","type","search","check"],"body":false,"upload":null,"file":true,"paged":false,"customer":false,"signedIn":true,"idempotent":false},
|
|
@@ -287,7 +311,12 @@ export const OPERATIONS = [
|
|
|
287
311
|
{"id":"customer.subscriptions.pause","name":"subscriptions.pause","method":"POST","path":"/stores/{storeId}/customer/subscriptions/{subscriptionId}/pause","params":["subscriptionId"],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":true,"signedIn":true,"idempotent":false},
|
|
288
312
|
{"id":"customer.subscriptions.resume","name":"subscriptions.resume","method":"POST","path":"/stores/{storeId}/customer/subscriptions/{subscriptionId}/resume","params":["subscriptionId"],"query":[],"body":false,"upload":null,"file":false,"paged":false,"customer":true,"signedIn":true,"idempotent":false},
|
|
289
313
|
{"id":"customer.subscriptions.card","name":"subscriptions.card","method":"POST","path":"/stores/{storeId}/customer/subscriptions/{subscriptionId}/card","params":["subscriptionId"],"query":[],"body":false,"upload":null,"file":false,"paged":false,"customer":true,"signedIn":true,"idempotent":false},
|
|
314
|
+
{"id":"customer.tickets.list","name":"tickets.list","method":"GET","path":"/stores/{storeId}/customer/tickets","params":[],"query":[],"body":false,"upload":null,"file":false,"paged":false,"customer":true,"signedIn":true,"idempotent":false},
|
|
315
|
+
{"id":"customer.tickets.create","name":"tickets.create","method":"POST","path":"/stores/{storeId}/customer/tickets","params":[],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":true,"signedIn":true,"idempotent":false},
|
|
316
|
+
{"id":"customer.tickets.show","name":"tickets.show","method":"GET","path":"/stores/{storeId}/customer/tickets/{ticketId}","params":["ticketId"],"query":[],"body":false,"upload":null,"file":false,"paged":false,"customer":true,"signedIn":true,"idempotent":false},
|
|
317
|
+
{"id":"customer.tickets.reply","name":"tickets.reply","method":"POST","path":"/stores/{storeId}/customer/tickets/{ticketId}/reply","params":["ticketId"],"query":[],"body":true,"upload":null,"file":false,"paged":false,"customer":true,"signedIn":true,"idempotent":false},
|
|
318
|
+
{"id":"customer.tickets.resolve","name":"tickets.resolve","method":"POST","path":"/stores/{storeId}/customer/tickets/{ticketId}/resolve","params":["ticketId"],"query":[],"body":false,"upload":null,"file":false,"paged":false,"customer":true,"signedIn":true,"idempotent":false},
|
|
290
319
|
];
|
|
291
320
|
|
|
292
321
|
/** Every webhook event, by name. */
|
|
293
|
-
export const EVENTS = ["order.created","order.payment_started","order.paid","order.delivered","order.redelivered","order.email_failed","order.cancelled","order.refunded","order.disputed","order.chargeback","order.payment_submitted","order.rerouted","review.created","review.removed","review.flag_declined","stock.low","stock.out","product.created","product.updated","product.deleted","customer.created","customer.balance_changed","affiliate.created","affiliate.payout_requested","affiliate.payout_answered","affiliate.balance_changed","subscription.started","subscription.renewed","subscription.cancelled","subscription.ended","subscription.paused","subscription.resumed","subscription.charge_failed","cart.email_sent","cart.recovered","cart.unsubscribed","routing.processor_paused","routing.processor_resumed","dispute.opened","dispute.updated","dispute.won","dispute.lost","dispute.closed","fraud.checkout_blocked","webhook.test"];
|
|
322
|
+
export const EVENTS = ["order.created","order.payment_started","order.paid","order.delivered","order.redelivered","order.email_failed","order.cancelled","order.refunded","order.disputed","order.chargeback","order.payment_submitted","order.rerouted","review.created","review.removed","review.flag_declined","stock.low","stock.out","product.created","product.updated","product.deleted","customer.created","customer.balance_changed","customer.tagged","customer.untagged","affiliate.created","affiliate.payout_requested","affiliate.payout_answered","affiliate.balance_changed","subscription.started","subscription.renewed","subscription.cancelled","subscription.ended","subscription.paused","subscription.resumed","subscription.charge_failed","cart.email_sent","cart.recovered","cart.unsubscribed","winback.email_sent","winback.won_back","subscriber.subscribed","subscriber.unsubscribed","routing.processor_paused","routing.processor_resumed","dispute.opened","dispute.updated","dispute.won","dispute.lost","dispute.closed","app.connected","app.disconnected","ticket.opened","ticket.message","ticket.resolved","ticket.reopened","fraud.checkout_blocked","webhook.test"];
|
package/index.d.ts
CHANGED
|
@@ -13,7 +13,10 @@ export * from "./generated/api.js";
|
|
|
13
13
|
export declare const VERSION: string;
|
|
14
14
|
|
|
15
15
|
export interface ClientOptions {
|
|
16
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* `shoply_sk_...`, made under Developers, API Keys, or an app's token (`shoply_at_...`) from
|
|
18
|
+
* `oauth.exchange()`. Left out, SHOPLY_API_KEY from the environment.
|
|
19
|
+
*/
|
|
17
20
|
apiKey?: string;
|
|
18
21
|
/** The store's ID. Left out, it is asked of the key the first time it is needed. */
|
|
19
22
|
storeId?: string;
|
|
@@ -118,6 +121,7 @@ export declare class Shoply {
|
|
|
118
121
|
static readonly VERSION: string;
|
|
119
122
|
static readonly EVENTS: readonly WebhookEventName[];
|
|
120
123
|
static readonly webhooks: Webhooks;
|
|
124
|
+
static readonly oauth: OAuth;
|
|
121
125
|
constructor(options?: ClientOptions);
|
|
122
126
|
/** The store the key belongs to, asked once and remembered. */
|
|
123
127
|
storeId(): Promise<string>;
|
|
@@ -183,4 +187,42 @@ export interface Webhooks {
|
|
|
183
187
|
|
|
184
188
|
export declare const webhooks: Webhooks;
|
|
185
189
|
|
|
190
|
+
/** What a code is swapped for: a token of the seller's store, until they disconnect the app. */
|
|
191
|
+
export interface OAuthToken {
|
|
192
|
+
/** `shoply_at_...`: pass it as `apiKey`. */
|
|
193
|
+
accessToken: string;
|
|
194
|
+
tokenType: "Bearer";
|
|
195
|
+
/** What the seller approved: every permission the app asks for. */
|
|
196
|
+
scopes: string[];
|
|
197
|
+
/** The store it is for. */
|
|
198
|
+
storeId: string;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export interface OAuth {
|
|
202
|
+
/** The page to send a seller to. `state` is yours, sent back unchanged: check it. */
|
|
203
|
+
authorizeUrl(options: { clientId: string; redirectUri: string; state?: string; codeChallenge?: string; dashboardURL?: string }): string;
|
|
204
|
+
/** A PKCE pair: keep `verifier` until the code comes back, and send `challenge`. */
|
|
205
|
+
pkce(): Promise<{ verifier: string; challenge: string; method: "S256" }>;
|
|
206
|
+
/** A random `state`, to keep with the seller's session and compare when they come back. */
|
|
207
|
+
state(): string;
|
|
208
|
+
/**
|
|
209
|
+
* The code swapped for the token: with the client secret on a server, or with the PKCE
|
|
210
|
+
* verifier from a desktop or phone app. Throws a ShoplyError whose `code` is OAuth's word
|
|
211
|
+
* (`invalid_grant`, `invalid_client`).
|
|
212
|
+
*/
|
|
213
|
+
exchange(options: {
|
|
214
|
+
clientId: string;
|
|
215
|
+
clientSecret?: string;
|
|
216
|
+
code: string;
|
|
217
|
+
redirectUri: string;
|
|
218
|
+
codeVerifier?: string;
|
|
219
|
+
baseURL?: string;
|
|
220
|
+
fetch?: typeof fetch;
|
|
221
|
+
}): Promise<OAuthToken>;
|
|
222
|
+
/** A token given up: it stops working at once. */
|
|
223
|
+
revoke(options: { clientId: string; clientSecret?: string; token: string; baseURL?: string; fetch?: typeof fetch }): Promise<void>;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export declare const oauth: OAuth;
|
|
227
|
+
|
|
186
228
|
export default Shoply;
|
package/index.js
CHANGED
|
@@ -8,11 +8,13 @@
|
|
|
8
8
|
//
|
|
9
9
|
// A route's id in the API reference is its method: `orders.show` is `shoply.orders.show()`.
|
|
10
10
|
// The Customer API, which a buyer's own device asks with a token of theirs, is ShoplyCustomer.
|
|
11
|
+
// An app connected to a seller's store by OAuth passes its token as `apiKey`, and `oauth` has
|
|
12
|
+
// the steps that get one.
|
|
11
13
|
// Docs: https://docs.shoply.gg/api-reference/node
|
|
12
14
|
|
|
13
15
|
import { EVENTS, OPERATIONS } from "./generated/operations.js";
|
|
14
16
|
|
|
15
|
-
export const VERSION = "0.
|
|
17
|
+
export const VERSION = "0.4.0";
|
|
16
18
|
|
|
17
19
|
const API = "https://api.shoply.gg/v1";
|
|
18
20
|
|
|
@@ -317,7 +319,7 @@ export class Shoply {
|
|
|
317
319
|
#storeId;
|
|
318
320
|
|
|
319
321
|
/**
|
|
320
|
-
* @param options.apiKey `shoply_sk_
|
|
322
|
+
* @param options.apiKey `shoply_sk_...`, or an app's token (`shoply_at_...`). Left out, SHOPLY_API_KEY from the environment.
|
|
321
323
|
* @param options.storeId The store's ID. Left out, it is asked of the key the first time it is needed.
|
|
322
324
|
* @param options.timeout How long one try waits for an answer, in milliseconds. 60 seconds.
|
|
323
325
|
* @param options.maxRetries How many more tries a request that can be repeated safely is given. 2.
|
|
@@ -332,8 +334,11 @@ export class Shoply {
|
|
|
332
334
|
|
|
333
335
|
const apiKey = options.apiKey ?? globalThis.process?.env?.SHOPLY_API_KEY;
|
|
334
336
|
|
|
335
|
-
if (typeof apiKey !== "string" || !apiKey.startsWith("shoply_sk_")) {
|
|
336
|
-
throw new ShoplyError({
|
|
337
|
+
if (typeof apiKey !== "string" || !(apiKey.startsWith("shoply_sk_") || apiKey.startsWith("shoply_at_"))) {
|
|
338
|
+
throw new ShoplyError({
|
|
339
|
+
message: "Pass the store's secret API key as `apiKey` (shoply_sk_...), or an app's token (shoply_at_...), or set SHOPLY_API_KEY.",
|
|
340
|
+
code: "api_key_required",
|
|
341
|
+
});
|
|
337
342
|
}
|
|
338
343
|
|
|
339
344
|
this.#storeId = options.storeId ? Promise.resolve(options.storeId) : null;
|
|
@@ -537,6 +542,131 @@ function same(a, b) {
|
|
|
537
542
|
|
|
538
543
|
Shoply.webhooks = webhooks;
|
|
539
544
|
|
|
545
|
+
// ── OAuth ──────────────────────────────────────────────────────────────────────────────
|
|
546
|
+
|
|
547
|
+
const DASHBOARD = "https://dashboard.shoply.gg";
|
|
548
|
+
|
|
549
|
+
const base64url = (bytes) => btoa(String.fromCharCode(...bytes)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
550
|
+
|
|
551
|
+
/** An OAuth answer that was an error, as a ShoplyError: `code` is OAuth's own word (`invalid_grant`...). */
|
|
552
|
+
async function oauthPost(path, form, { clientId, clientSecret, baseURL, fetch: fetchFn }) {
|
|
553
|
+
const headers = { "Content-Type": "application/x-www-form-urlencoded", Accept: "application/json", "User-Agent": userAgent() };
|
|
554
|
+
|
|
555
|
+
if (clientSecret) {
|
|
556
|
+
headers.Authorization = `Basic ${btoa(`${encodeURIComponent(clientId)}:${encodeURIComponent(clientSecret)}`)}`;
|
|
557
|
+
} else {
|
|
558
|
+
form.client_id = clientId;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
let answer;
|
|
562
|
+
|
|
563
|
+
try {
|
|
564
|
+
answer = await (fetchFn ?? globalThis.fetch)(`${String(baseURL ?? API).replace(/\/+$/, "")}${path}`, {
|
|
565
|
+
method: "POST",
|
|
566
|
+
headers,
|
|
567
|
+
body: new URLSearchParams(form).toString(),
|
|
568
|
+
});
|
|
569
|
+
} catch (cause) {
|
|
570
|
+
throw new ShoplyError({ message: "Shoply did not answer.", code: "connection_error", cause });
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
const text = await answer.text();
|
|
574
|
+
let body = null;
|
|
575
|
+
|
|
576
|
+
try {
|
|
577
|
+
body = text === "" ? {} : JSON.parse(text);
|
|
578
|
+
} catch {
|
|
579
|
+
body = null;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
if (!answer.ok) {
|
|
583
|
+
throw new ShoplyError({
|
|
584
|
+
status: answer.status,
|
|
585
|
+
code: body?.error ?? (answer.status === 429 ? "rate_limited" : "oauth_error"),
|
|
586
|
+
message: body?.error_description ?? body?.message ?? `Shoply answered ${answer.status}.`,
|
|
587
|
+
retryAfter: Number(answer.headers.get("retry-after")) || null,
|
|
588
|
+
body,
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
return body ?? {};
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* Connecting an app to a seller's store, by OAuth's authorization code flow:
|
|
597
|
+
*
|
|
598
|
+
* 1. Send the seller to `authorizeUrl()`. With PKCE (always, for a desktop or phone app),
|
|
599
|
+
* make a pair with `pkce()` first, keep its verifier and send its challenge.
|
|
600
|
+
* 2. They come back to your redirect URL with `code` and your `state`. Check the state.
|
|
601
|
+
* 3. `exchange()` the code, on your server with the client secret, or with the verifier.
|
|
602
|
+
* 4. The token is a key of their store: `new Shoply({ apiKey: token })`. It works until the
|
|
603
|
+
* seller disconnects the app; `revoke()` gives it up.
|
|
604
|
+
*/
|
|
605
|
+
export const oauth = {
|
|
606
|
+
/** The page to send a seller to. `state` is yours, sent back unchanged: check it. */
|
|
607
|
+
authorizeUrl({ clientId, redirectUri, state, codeChallenge, dashboardURL = DASHBOARD } = {}) {
|
|
608
|
+
if (!clientId || !redirectUri) throw new TypeError("Pass the app's clientId and the redirectUri the seller is sent back to.");
|
|
609
|
+
|
|
610
|
+
const url = new URL("/oauth/authorize/", dashboardURL);
|
|
611
|
+
|
|
612
|
+
url.searchParams.set("response_type", "code");
|
|
613
|
+
url.searchParams.set("client_id", clientId);
|
|
614
|
+
url.searchParams.set("redirect_uri", redirectUri);
|
|
615
|
+
if (state) url.searchParams.set("state", state);
|
|
616
|
+
|
|
617
|
+
if (codeChallenge) {
|
|
618
|
+
url.searchParams.set("code_challenge", codeChallenge);
|
|
619
|
+
url.searchParams.set("code_challenge_method", "S256");
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
return url.toString();
|
|
623
|
+
},
|
|
624
|
+
|
|
625
|
+
/** A PKCE pair: keep `verifier` until the code comes back, and send `challenge`. */
|
|
626
|
+
async pkce() {
|
|
627
|
+
const verifier = base64url(globalThis.crypto.getRandomValues(new Uint8Array(32)));
|
|
628
|
+
const challenge = base64url(new Uint8Array(await globalThis.crypto.subtle.digest("SHA-256", encoder.encode(verifier))));
|
|
629
|
+
|
|
630
|
+
return { verifier, challenge, method: "S256" };
|
|
631
|
+
},
|
|
632
|
+
|
|
633
|
+
/** A random `state`, to keep with the seller's session and compare when they come back. */
|
|
634
|
+
state() {
|
|
635
|
+
return base64url(globalThis.crypto.getRandomValues(new Uint8Array(16)));
|
|
636
|
+
},
|
|
637
|
+
|
|
638
|
+
/**
|
|
639
|
+
* The code swapped for the token. A web app sends its client secret; a desktop or phone app
|
|
640
|
+
* sends the PKCE verifier and no secret. Throws a ShoplyError whose `code` is OAuth's word:
|
|
641
|
+
* `invalid_grant` (the code is wrong, used, or older than ten minutes), `invalid_client`.
|
|
642
|
+
*/
|
|
643
|
+
async exchange({ clientId, clientSecret, code, redirectUri, codeVerifier, baseURL, fetch } = {}) {
|
|
644
|
+
if (!clientId || !code || !redirectUri) throw new TypeError("Pass clientId, the code and the redirectUri it came back to.");
|
|
645
|
+
|
|
646
|
+
const form = { grant_type: "authorization_code", code, redirect_uri: redirectUri };
|
|
647
|
+
|
|
648
|
+
if (codeVerifier) form.code_verifier = codeVerifier;
|
|
649
|
+
|
|
650
|
+
const body = await oauthPost("/oauth/token", form, { clientId, clientSecret, baseURL, fetch });
|
|
651
|
+
|
|
652
|
+
return {
|
|
653
|
+
accessToken: body.access_token,
|
|
654
|
+
tokenType: body.token_type,
|
|
655
|
+
scopes: String(body.scope ?? "").split(" ").filter(Boolean),
|
|
656
|
+
storeId: body.store_id,
|
|
657
|
+
};
|
|
658
|
+
},
|
|
659
|
+
|
|
660
|
+
/** A token given up: it stops working at once. Answered the same whether or not there was one. */
|
|
661
|
+
async revoke({ clientId, clientSecret, token, baseURL, fetch } = {}) {
|
|
662
|
+
if (!clientId || !token) throw new TypeError("Pass clientId and the token to give up.");
|
|
663
|
+
|
|
664
|
+
await oauthPost("/oauth/revoke", { token }, { clientId, clientSecret, baseURL, fetch });
|
|
665
|
+
},
|
|
666
|
+
};
|
|
667
|
+
|
|
668
|
+
Shoply.oauth = oauth;
|
|
669
|
+
|
|
540
670
|
function userAgent() {
|
|
541
671
|
const runtime = globalThis.process?.versions?.node ? `node/${globalThis.process.versions.node}` : "unknown";
|
|
542
672
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shoplygg/node",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Shoply's API from your own server: every route as a typed method, safe retries, pages, and
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Shoply's API from your own server: every route as a typed method, safe retries, pages, checking webhooks, and OAuth for apps.",
|
|
5
5
|
"keywords": ["shoply", "api", "sdk", "ecommerce", "webhooks", "digital-goods"],
|
|
6
6
|
"homepage": "https://docs.shoply.gg/api-reference/node",
|
|
7
7
|
"repository": {
|