@shoplygg/node 0.0.0-stage → 0.1.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 CHANGED
@@ -1,3 +1,78 @@
1
- # Temporary Holding Version
1
+ # @shoplygg/node
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Shoply's API from your own server. Every route of the [API reference](https://docs.shoply.gg/api-reference/introduction) is a method, with TypeScript types, safe retries, lists a page at a time or all at once, and checking that a webhook came from Shoply.
4
+
5
+ ```sh
6
+ npm install @shoplygg/node
7
+ ```
8
+
9
+ Node 20 or later. No dependencies.
10
+
11
+ ```js
12
+ import Shoply from "@shoplygg/node";
13
+
14
+ const shoply = new Shoply({ apiKey: process.env.SHOPLY_API_KEY });
15
+
16
+ const order = await shoply.orders.show("ORDER_ID");
17
+
18
+ for await (const customer of shoply.customers.list({ search: "gmail.com" })) {
19
+ console.log(customer.emailAddress);
20
+ }
21
+
22
+ const coupon = await shoply.coupons.create({ code: "SPRING10", value: 10 });
23
+ ```
24
+
25
+ Make the key under **Developers**, **API Keys** in the dashboard, with the permissions your code needs. It is a secret: the client refuses to start in a web page.
26
+
27
+ A route's method is its id: **Get an order** is `orders.show`, so `shoply.orders.show(orderId)`. The address's IDs come first, in order, then the fields as one object, then options.
28
+
29
+ ## Errors
30
+
31
+ A refusal is a `ShoplyError`, with `status`, `code` (`not_found`, `missing_scope`, `validation_failed`...), `errors` for each field that is wrong, and `requestId`.
32
+
33
+ ```js
34
+ import { ShoplyError } from "@shoplygg/node";
35
+
36
+ try {
37
+ await shoply.coupons.create({ code: "SPRING10", value: 10 });
38
+ } catch (error) {
39
+ if (error instanceof ShoplyError && error.code === "validation_failed") console.log(error.errors);
40
+ }
41
+ ```
42
+
43
+ A change is sent with an `Idempotency-Key`, the same on every try, so a try whose answer was lost is never carried out twice. Pass `{ idempotencyKey }` in the options, the last argument, to use your own.
44
+
45
+ ## Webhooks
46
+
47
+ ```js
48
+ import express from "express";
49
+ import { webhooks } from "@shoplygg/node";
50
+
51
+ const app = express();
52
+
53
+ app.post("/shoply", express.raw({ type: "application/json" }), async (req, res) => {
54
+ const event = await webhooks.verify(req.body, req.headers, process.env.SHOPLY_WEBHOOK_SECRET);
55
+
56
+ if (event.event === "order.paid") {
57
+ // event.data is the order
58
+ }
59
+
60
+ res.sendStatus(200);
61
+ });
62
+ ```
63
+
64
+ ## The Customer API
65
+
66
+ `ShoplyCustomer` signs a buyer in with a code emailed to them, and reads their own orders, keys and subscriptions. It holds the buyer's token and no key, so it is safe in a browser or an app.
67
+
68
+ ```js
69
+ import { ShoplyCustomer } from "@shoplygg/node";
70
+
71
+ const buyer = new ShoplyCustomer({ storeId: "YOUR_STORE_ID" });
72
+
73
+ await buyer.login({ email });
74
+ await buyer.verify({ email, code });
75
+ const me = await buyer.show();
76
+ ```
77
+
78
+ The whole guide: https://docs.shoply.gg/api-reference/node