Edits history of script submission #17908 for ' List translations by locale (phrase)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Phrase = {
    	token: string
    	baseUrl: string
    }
    /**
     * List translations by locale
     * List translations for a specific locale. If you want to download all translations for one locale we recommend to use the locales#download endpoint.
     */
    export async function main(
    	auth: Phrase,
    	project_id: string,
    	locale_id: string,
    	page: string | undefined,
    	per_page: string | undefined,
    	branch: string | undefined,
    	sort: string | undefined,
    	order: string | undefined,
    	q: string | undefined
    ) {
    	const url = new URL(`${auth.baseUrl}/projects/${project_id}/locales/${locale_id}/translations`)
    	for (const [k, v] of [
    		['page', page],
    		['per_page', per_page],
    		['branch', branch],
    		['sort', sort],
    		['order', order],
    		['q', q]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'GET',
    		headers: {
    			Authorization: 'ApiToken ' + auth.token
    		},
    		body: undefined
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 235 days ago