Edits history of script submission #15363 for ' Attach a file to contract (deel)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Base64 = string
    /**
     * Attach a file to contract
     * Attach a file to contract document.
     **Token scopes**: `contracts:write`
     */
    export async function main(auth: RT.Deel, contract_id: string, body: Body) {
    	const url = new URL(`https://api.letsdeel.com/rest/v2/contracts/${contract_id}/documents`)
    
    	const formData = new FormData()
    	for (const [k, v] of Object.entries(body)) {
    		if (v !== undefined && v !== '') {
    			if (['file'].includes(k)) {
    				const { base64, type, name } = v as {
    					base64: Base64
    					type: string
    					name: string
    				}
    				formData.append(
    					k,
    					new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], { type }),
    					name
    				)
    			} else {
    				formData.append(k, String(v))
    			}
    		}
    	}
    	const response = await fetch(url, {
    		method: 'POST',
    		headers: {
    			Authorization: 'Bearer ' + auth.apiKey
    		},
    		body: formData
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    
    /* eslint-disable */
    /**
     * This file was automatically generated by json-schema-to-typescript.
     * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
     * and run json-schema-to-typescript to regenerate this file.
     */
    
    /**
     * This is the file you will upload in a multi-part form.
     */
    export interface Body {
    	/**
    	 * Upload the file you want to attach to this entry.
    	 */
    	file?: {
    		base64: Base64
    		type:
    			| 'image/png'
    			| 'image/jpeg'
    			| 'image/gif'
    			| 'application/pdf'
    			| 'appication/json'
    			| 'text/csv'
    			| 'text/plain'
    			| 'audio/mpeg'
    			| 'audio/wav'
    			| 'video/mp4'
    		name: string
    	}
    	[k: string]: unknown
    }
    

    Submitted by hugo697 235 days ago