Edits history of script submission #17818 for ' Download a locale (phrase)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Phrase = {
    	token: string
    	baseUrl: string
    }
    /**
     * Download a locale
     * Download a locale in a specific file format.
     */
    export async function main(
    	auth: Phrase,
    	project_id: string,
    	id: string,
    	branch: string | undefined,
    	file_format: string | undefined,
    	tags: string | undefined,
    	tag: string | undefined,
    	include_empty_translations: string | undefined,
    	exclude_empty_zero_forms: string | undefined,
    	include_translated_keys: string | undefined,
    	keep_notranslate_tags: string | undefined,
    	convert_emoji: string | undefined,
    	format_options: any,
    	encoding: string | undefined,
    	skip_unverified_translations: string | undefined,
    	include_unverified_translations: string | undefined,
    	use_last_reviewed_version: string | undefined,
    	fallback_locale_id: string | undefined,
    	source_locale_id: string | undefined,
    	translation_key_prefix: string | undefined,
    	filter_by_prefix: string | undefined,
    	custom_metadata_filters: any,
    	If_Modified_Since: string,
    	If_None_Match: string
    ) {
    	const url = new URL(`${auth.baseUrl}/projects/${project_id}/locales/${id}/download`)
    	for (const [k, v] of [
    		['branch', branch],
    		['file_format', file_format],
    		['tags', tags],
    		['tag', tag],
    		['include_empty_translations', include_empty_translations],
    		['exclude_empty_zero_forms', exclude_empty_zero_forms],
    		['include_translated_keys', include_translated_keys],
    		['keep_notranslate_tags', keep_notranslate_tags],
    		['convert_emoji', convert_emoji],
    		['encoding', encoding],
    		['skip_unverified_translations', skip_unverified_translations],
    		['include_unverified_translations', include_unverified_translations],
    		['use_last_reviewed_version', use_last_reviewed_version],
    		['fallback_locale_id', fallback_locale_id],
    		['source_locale_id', source_locale_id],
    		['translation_key_prefix', translation_key_prefix],
    		['filter_by_prefix', filter_by_prefix]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	encodeParams({ format_options, custom_metadata_filters }).forEach((v, k) => {
    		if (v !== undefined && v !== '') {
    			url.searchParams.append(k, v)
    		}
    	})
    	const response = await fetch(url, {
    		method: 'GET',
    		headers: {
    			'If-Modified-Since': If_Modified_Since,
    			'If-None-Match': If_None_Match,
    			Authorization: 'ApiToken ' + auth.token
    		},
    		body: undefined
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.text()
    }
    
    function encodeParams(o: any) {
    	function iter(o: any, path: string) {
    		if (Array.isArray(o)) {
    			o.forEach(function (a) {
    				iter(a, path + '[]')
    			})
    			return
    		}
    		if (o !== null && typeof o === 'object') {
    			Object.keys(o).forEach(function (k) {
    				iter(o[k], path + '[' + k + ']')
    			})
    			return
    		}
    		data.push(path + '=' + o)
    	}
    	const data: string[] = []
    	Object.keys(o).forEach(function (k) {
    		if (o[k] !== undefined) {
    			iter(o[k], k)
    		}
    	})
    	return new URLSearchParams(data.join('&'))
    }
    

    Submitted by hugo697 235 days ago