1 | type Shopify = { |
2 | token: string; |
3 | store_name: string; |
4 | }; |
5 | |
6 | * Creates an order |
7 | * Creates an order. |
8 | */ |
9 | export async function main( |
10 | auth: Shopify, |
11 | api_version: string = "2023-10", |
12 | body: { |
13 | order?: { |
14 | email?: string; |
15 | fulfillment_status?: string; |
16 | fulfillments?: { location_id?: number; [k: string]: unknown }[]; |
17 | line_items?: { |
18 | quantity?: number; |
19 | variant_id?: number; |
20 | [k: string]: unknown; |
21 | }[]; |
22 | [k: string]: unknown; |
23 | }; |
24 | [k: string]: unknown; |
25 | } |
26 | ) { |
27 | const url = new URL( |
28 | `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/orders.json` |
29 | ); |
30 |
|
31 | const response = await fetch(url, { |
32 | method: "POST", |
33 | headers: { |
34 | "Content-Type": "application/json", |
35 | "X-Shopify-Access-Token": auth.token, |
36 | }, |
37 | body: JSON.stringify(body), |
38 | }); |
39 | if (!response.ok) { |
40 | const text = await response.text(); |
41 | throw new Error(`${response.status} ${text}`); |
42 | } |
43 | return await response.json(); |
44 | } |
45 |
|