@shopify/react-network
Version:
A collection of components that allow you to set common HTTP headers from within your React application
41 lines (36 loc) • 1.04 kB
JavaScript
export { EFFECT_ID, NetworkManager } from './manager.mjs';
export { NetworkContext } from './context.mjs';
function applyToContext(ctx, manager) {
const {
status,
redirectUrl,
headers,
cookies
} = manager.extract();
if (redirectUrl) {
ctx.redirect(redirectUrl);
}
if (status) {
ctx.status = status;
}
for (const [header, value] of headers) {
ctx.set(header, value);
}
Object.entries(cookies).forEach(([cookie, options]) => {
const {
value,
...cookieOptions
} = options;
// only set cookies that have been changed
// decode URI as the manager's cookie value is also decoded
const rawCookieValue = ctx.cookies.get(cookie);
if (rawCookieValue != null && decodeURIComponent(rawCookieValue) === value) {
return;
}
// missing 'none` in `sameSite`
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/cookies/index.d.ts#L91
ctx.cookies.set(cookie, value, cookieOptions);
});
return ctx;
}
export { applyToContext };