1 | type Shopify = { |
2 | token: string; |
3 | store_name: string; |
4 | }; |
5 | |
6 | * Creates a discount code |
7 | * Creates a discount code |
8 | */ |
9 | export async function main( |
10 | auth: Shopify, |
11 | api_version: string = "2023-10", |
12 | price_rule_id: string, |
13 | body: { |
14 | discount_code?: { code?: string; [k: string]: unknown }; |
15 | [k: string]: unknown; |
16 | } |
17 | ) { |
18 | const url = new URL( |
19 | `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/price_rules/${price_rule_id}/discount_codes.json` |
20 | ); |
21 |
|
22 | const response = await fetch(url, { |
23 | method: "POST", |
24 | headers: { |
25 | "Content-Type": "application/json", |
26 | "X-Shopify-Access-Token": auth.token, |
27 | }, |
28 | body: JSON.stringify(body), |
29 | }); |
30 | if (!response.ok) { |
31 | const text = await response.text(); |
32 | throw new Error(`${response.status} ${text}`); |
33 | } |
34 | return await response.json(); |
35 | } |
36 |
|