Edits history of script submission #10181 for ' Creates one or more items (xero)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Xero = {
    	token: string
    }
    /**
     * Creates one or more items
     *
     */
    export async function main(
    	auth: Xero,
    	summarizeErrors: string | undefined,
    	unitdp: string | undefined,
    	xero_tenant_id: string,
    	Idempotency_Key: string,
    	body: {
    		Items?: {
    			Code: string
    			InventoryAssetAccountCode?: string
    			Name?: string
    			IsSold?: false | true
    			IsPurchased?: false | true
    			Description?: string
    			PurchaseDescription?: string
    			PurchaseDetails?: {
    				UnitPrice?: number
    				AccountCode?: string
    				COGSAccountCode?: string
    				TaxType?: string
    			}
    			SalesDetails?: {
    				UnitPrice?: number
    				AccountCode?: string
    				COGSAccountCode?: string
    				TaxType?: string
    			}
    			IsTrackedAsInventory?: false | true
    			TotalCostPool?: number
    			QuantityOnHand?: number
    			UpdatedDateUTC?: string
    			ItemID?: string
    			StatusAttributeString?: string
    			ValidationErrors?: { Message?: string }[]
    		}[]
    	}
    ) {
    	const url = new URL(`https://api.xero.com/api.xro/2.0/Items`)
    	for (const [k, v] of [
    		['summarizeErrors', summarizeErrors],
    		['unitdp', unitdp]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'PUT',
    		headers: {
          Accept: 'application/json',
    			'xero-tenant-id': xero_tenant_id,
    			'Idempotency-Key': Idempotency_Key,
    			'Content-Type': 'application/json',
    			Authorization: 'Bearer ' + auth.token
    		},
    		body: JSON.stringify(body)
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 515 days ago