Skip to content

Commit

Permalink
Update unpkg with new property options
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloSzx committed Jan 4, 2024
1 parent ced9cc1 commit 1bf75f7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-foxes-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-ez/plugin-altair': patch
---

Update unpkg with new property options
58 changes: 31 additions & 27 deletions packages/plugin/altair/src/render/unpkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,52 @@ if (typeof fetch !== 'undefined') {
fetchFn = import('cross-undici-fetch').then(v => v.fetch);
}

function getObjectPropertyForOption(option: any, propertyName: keyof AltairConfigOptions) {
if (option) {
const getRenderedAltairOpts = (renderOptions: RenderOptions, keys: (keyof AltairConfigOptions)[]) => {
const optProps = Object.keys(renderOptions)
.filter((key): key is keyof AltairConfigOptions => keys.includes(key as keyof AltairConfigOptions))
.map(key => getObjectPropertyForOption(renderOptions[key], key));

return ['{', ...optProps, '}'].join('\n');
};
function getObjectPropertyForOption(option: unknown, propertyName: keyof AltairConfigOptions) {
if (typeof option !== 'undefined') {
switch (typeof option) {
case 'object':
return `${propertyName}: ${JSON.stringify(option)},`;
case 'boolean':
return `${propertyName}: ${option},`;
}
return `${propertyName}: \`${option}\`,`;
}
return '';
}

const getRenderedAltairOpts = (renderOptions: RenderOptions, keys: (keyof AltairConfigOptions)[]) => {
const optProps = Object.keys(renderOptions)
.filter((key: any): key is keyof AltairConfigOptions => keys.includes(key))
.map(key => getObjectPropertyForOption(renderOptions[key], key));

return ['{', ...optProps, '}'].join('\n');
};

/**
* Render Altair Initial options as a string using the provided renderOptions
* @param renderOptions
*/
export const renderInitialOptions = (options: RenderOptions = {}) => {
return `
AltairGraphQL.init(${getRenderedAltairOpts(options, [
'endpointURL',
'subscriptionsEndpoint',
'initialQuery',
'initialVariables',
'initialPreRequestScript',
'initialPostRequestScript',
'initialHeaders',
'initialEnvironments',
'instanceStorageNamespace',
'initialSettings',
'initialSubscriptionsProvider',
'initialSubscriptionsPayload',
'preserveState',
'initialHttpMethod',
])});
`;
AltairGraphQL.init(${getRenderedAltairOpts(options, [
'endpointURL',
'subscriptionsEndpoint',
'subscriptionsProtocol',
'initialQuery',
'initialVariables',
'initialPreRequestScript',
'initialPostRequestScript',
'initialHeaders',
'initialEnvironments',
'instanceStorageNamespace',
'initialSettings',
'initialSubscriptionsProvider',
'initialSubscriptionsPayload',
'preserveState',
'initialHttpMethod',
'initialWindows',
'disableAccount',
])});
`;
};

// const fetchFn = typeof fetch !== 'undefined' ? fetch : crossFetch;
Expand Down

0 comments on commit 1bf75f7

Please sign in to comment.