UNPKG

mod-arch-core

Version:

Core functionality and API utilities for modular architecture micro-frontend projects

96 lines 3.77 kB
export const mergeRequestInit = (opts = {}, specificOpts = {}) => ({ ...specificOpts, ...(opts.signal && { signal: opts.signal }), headers: { ...(opts.headers ?? {}), ...(specificOpts.headers ?? {}), }, }); const callRestJSON = (host, path, requestInit, { data, fileContents, queryParams, parseJSON = true }) => { const { method, ...otherOptions } = requestInit; const sanitizedQueryParams = queryParams ? Object.entries(queryParams).reduce((acc, [key, value]) => { if (value) { return { ...acc, [key]: value }; } return acc; }, {}) : null; const searchParams = sanitizedQueryParams ? new URLSearchParams(sanitizedQueryParams).toString() : null; let requestData; let contentType; let formData; if (fileContents) { formData = new FormData(); formData.append('uploadfile', new Blob([fileContents], { type: 'application/x-yaml' }), 'uploadedFile.yml'); } else if (data) { // It's OK for contentType and requestData to BOTH be undefined for e.g. a GET request or POST with no body. contentType = 'application/json;charset=UTF-8'; requestData = JSON.stringify(data); } return fetch(`${host}${path}${searchParams ? `?${searchParams}` : ''}`, { ...otherOptions, headers: { ...otherOptions.headers, ...(contentType && { 'Content-Type': contentType }), }, method, body: formData ?? requestData, }).then((response) => response.text().then((fetchedData) => { if (parseJSON) { return JSON.parse(fetchedData); } return fetchedData; })); }; export const restGET = (host, path, queryParams = {}, options) => callRestJSON(host, path, mergeRequestInit(options, { method: 'GET' }), { queryParams, parseJSON: options?.parseJSON, }); /** Standard POST */ export const restCREATE = (host, path, data, queryParams = {}, options) => callRestJSON(host, path, mergeRequestInit(options, { method: 'POST' }), { data, queryParams, parseJSON: options?.parseJSON, }); /** POST -- but with file content instead of body data */ export const restFILE = (host, path, fileContents, queryParams = {}, options) => callRestJSON(host, path, mergeRequestInit(options, { method: 'POST' }), { fileContents, queryParams, parseJSON: options?.parseJSON, }); /** POST -- but no body data -- targets simple endpoints */ export const restENDPOINT = (host, path, queryParams = {}, options) => callRestJSON(host, path, mergeRequestInit(options, { method: 'POST' }), { queryParams, parseJSON: options?.parseJSON, }); export const restUPDATE = (host, path, data, queryParams = {}, options) => callRestJSON(host, path, mergeRequestInit(options, { method: 'PUT' }), { data, queryParams, parseJSON: options?.parseJSON, }); export const restPATCH = (host, path, data, queryParams = {}, options) => callRestJSON(host, path, mergeRequestInit(options, { method: 'PATCH' }), { data, queryParams, parseJSON: options?.parseJSON, }); export const restDELETE = (host, path, data, queryParams = {}, options) => callRestJSON(host, path, mergeRequestInit(options, { method: 'DELETE' }), { data, queryParams, parseJSON: options?.parseJSON, }); export const isModArchResponse = (response) => { if (typeof response === 'object' && response !== null) { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const modArchBody = response; return modArchBody.data !== undefined; } return false; }; export const assembleModArchBody = (data) => ({ data, }); //# sourceMappingURL=apiUtils.js.map