Edits history of script submission #20330 for ' Upload a new project release file (sentry)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Base64 = string
    /**
     * Upload a new project release file
     * Upload a new file for the given release.
    
    Unlike other API requests, files must be uploaded using the traditional multipart/form-data content-type.
    
    Requests to this endpoint should use the region-specific domain eg. `us.sentry.io` or `de.sentry.io`
    
    The optional 'name' attribute should reflect the absolute path that this file will be referenced as. For example, in the case of JavaScript you might specify the full web URI.
     */
    export async function main(
    	auth: RT.Sentry,
    	project_id_or_slug: string,
    	version: string,
    	body: Body
    ) {
    	const url = new URL(
    		`https://${auth.region}.sentry.io/api/0/projects/${auth.organizationSlug}/${project_id_or_slug}/releases/${version}/files/`
    	)
    
    	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.token
    		},
    		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.
     */
    
    export interface Body {
    	/**
    	 * The multipart encoded file.
    	 */
    	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
    	}
    	/**
    	 * The name (full path) of the file.
    	 */
    	name?: string
    	/**
    	 * The name of the dist.
    	 */
    	dist?: string
    	/**
    	 * This parameter can be supplied multiple times to attach headers to the file. Each header is a string in the format `key:value`. For instance it can be used to define a content type.
    	 */
    	header?: string
    	[k: string]: unknown
    }
    

    Submitted by hugo697 235 days ago