Creates one or more items
One script reply has been approved by the moderators Verified
Created by hugo697 448 days ago Picked 2 times
Submitted by hugo697 Bun
Verified 448 days ago
1
//native
2
type Xero = {
3
	token: string
4
}
5
/**
6
 * Creates one or more items
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	summarizeErrors: string | undefined,
12
	unitdp: string | undefined,
13
	xero_tenant_id: string,
14
	Idempotency_Key: string,
15
	body: {
16
		Items?: {
17
			Code: string
18
			InventoryAssetAccountCode?: string
19
			Name?: string
20
			IsSold?: false | true
21
			IsPurchased?: false | true
22
			Description?: string
23
			PurchaseDescription?: string
24
			PurchaseDetails?: {
25
				UnitPrice?: number
26
				AccountCode?: string
27
				COGSAccountCode?: string
28
				TaxType?: string
29
			}
30
			SalesDetails?: {
31
				UnitPrice?: number
32
				AccountCode?: string
33
				COGSAccountCode?: string
34
				TaxType?: string
35
			}
36
			IsTrackedAsInventory?: false | true
37
			TotalCostPool?: number
38
			QuantityOnHand?: number
39
			UpdatedDateUTC?: string
40
			ItemID?: string
41
			StatusAttributeString?: string
42
			ValidationErrors?: { Message?: string }[]
43
		}[]
44
	}
45
) {
46
	const url = new URL(`https://api.xero.com/api.xro/2.0/Items`)
47
	for (const [k, v] of [
48
		['summarizeErrors', summarizeErrors],
49
		['unitdp', unitdp]
50
	]) {
51
		if (v !== undefined && v !== '' && k !== undefined) {
52
			url.searchParams.append(k, v)
53
		}
54
	}
55
	const response = await fetch(url, {
56
		method: 'PUT',
57
		headers: {
58
      Accept: 'application/json',
59
			'xero-tenant-id': xero_tenant_id,
60
			'Idempotency-Key': Idempotency_Key,
61
			'Content-Type': 'application/json',
62
			Authorization: 'Bearer ' + auth.token
63
		},
64
		body: JSON.stringify(body)
65
	})
66
	if (!response.ok) {
67
		const text = await response.text()
68
		throw new Error(`${response.status} ${text}`)
69
	}
70
	return await response.json()
71
}
72