|
|
import debugFactory from 'debug'; |
|
|
import { GP_BASE_URL } from './constants'; |
|
|
|
|
|
const debug = debugFactory( 'calypso:i18n-utils:glotpress' ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function postRequest( glotPressUrl, postFormData ) { |
|
|
const response = await window.fetch( glotPressUrl, { |
|
|
method: 'POST', |
|
|
credentials: 'include', |
|
|
body: postFormData, |
|
|
headers: { |
|
|
'Content-Type': 'application/x-www-form-urlencoded', |
|
|
}, |
|
|
} ); |
|
|
if ( response.ok ) { |
|
|
return await response.json(); |
|
|
} |
|
|
|
|
|
|
|
|
throw new Error( await response.body ); |
|
|
} |
|
|
|
|
|
export function encodeOriginalKey( { original, context } ) { |
|
|
return context + '\u0004' + original; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function recordOriginals( originalKeys, recordId, post = postRequest ) { |
|
|
const glotPressUrl = `${ GP_BASE_URL }/api/translations/-record-originals`; |
|
|
const recordIdQueryFragment = recordId ? `record_id=${ encodeURIComponent( recordId ) }&` : ''; |
|
|
const postFormData = |
|
|
recordIdQueryFragment + `originals=${ encodeURIComponent( JSON.stringify( originalKeys ) ) }`; |
|
|
|
|
|
return post( glotPressUrl, postFormData ).catch( ( err ) => |
|
|
debug( 'recordOriginals failed:', err ) |
|
|
); |
|
|
} |
|
|
|