Skip to content

Commit

Permalink
Switch to querystring approach
Browse files Browse the repository at this point in the history
  • Loading branch information
apai4 committed May 6, 2024
1 parent 4bc2028 commit 29464eb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
8 changes: 2 additions & 6 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function genID() {
}

export function openUrl(route: Route, options?: OpenOptions) {
return `${getOrigin(options)}${getOrg(options)}${route}${buildParams(options)}`;
return `${getOrigin(options)}${route}${buildParams(options)}`;
}

export function embedUrl(route: Route, options?: EmbedOptions) {
Expand All @@ -23,18 +23,14 @@ export function embedUrl(route: Route, options?: EmbedOptions) {
if (options && typeof options === 'object') {
Object.assign(config, options);
}
return `${getOrigin(config)}${getOrg(options)}${route}${buildParams(config)}`;
return `${getOrigin(config)}${route}${buildParams(config)}`;
}

function getOrigin(options: OpenOptions & EmbedOptions = {}) {
const origin = typeof options.origin === 'string' ? options.origin : DEFAULT_ORIGIN;
return origin.replace(/\/$/, '');
}

function getOrg(options: OpenOptions & EmbedOptions = {}){
return options?.organization ? `/orgs/${options.organization}` : '';
}

export function replaceAndEmbed(
target: HTMLElement,
frame: HTMLIFrameElement,
Expand Down
5 changes: 4 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ export interface ProjectOptions {
*
* Defaults to no organization.
*/
organization?: string;
organization?: {
provider: 'github';
name: string;
}
/**
* Show the sidebar as open or closed on page load.
*
Expand Down
7 changes: 5 additions & 2 deletions src/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { UI_SIDEBAR_VIEWS, UI_THEMES, UI_VIEWS } from './constants';

export type ParamOptions = Omit<
OpenOptions & EmbedOptions,
'origin' | 'newWindow' | 'height' | 'width' | 'organization'
'origin' | 'newWindow' | 'height' | 'width'
>;

/**
Expand Down Expand Up @@ -36,7 +36,9 @@ type ParamName =
| 'terminalHeight'
| 'theme'
| 'view'
| 'zenMode';
| 'zenMode'
| 'orgName'
| 'orgProvider';

export const generators: Record<keyof ParamOptions, (value: any) => string> = {
clickToLoad: (value: ParamOptions['clickToLoad']) => trueParam('ctl', value),
Expand All @@ -54,6 +56,7 @@ export const generators: Record<keyof ParamOptions, (value: any) => string> = {
theme: (value: ParamOptions['theme']) => enumParam('theme', value, UI_THEMES),
view: (value: ParamOptions['view']) => enumParam('view', value, UI_VIEWS),
zenMode: (value: ParamOptions['zenMode']) => trueParam('zenMode', value),
organization: (value: ParamOptions['organization']) => `${stringParams('orgName', value?.name)}&${stringParams('orgProvider', value?.provider)}`,
};

export function buildParams(options: ParamOptions = {}): string {
Expand Down
4 changes: 4 additions & 0 deletions test/unit/params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ describe('buildParams', () => {
hideExplorer: false,
hideNavigation: false,
openFile: '',
organization: undefined,
showSidebar: undefined,
sidebarView: 'default',
startScript: undefined,
Expand All @@ -107,6 +108,7 @@ describe('buildParams', () => {
hideExplorer: true,
hideNavigation: true,
openFile: ['src/index.js,src/styles.css', 'package.json'],
organization: {name: 'stackblitz', provider: 'github'},
showSidebar: true,
sidebarView: 'search',
startScript: 'dev:serve',
Expand All @@ -129,6 +131,8 @@ describe('buildParams', () => {
'hideNavigation=1',
'file=src%2Findex.js%2Csrc%2Fstyles.css',
'file=package.json',
'orgName=stackblitz',
'orgProvider=github',
'showSidebar=1',
'sidebarView=search',
'startScript=dev%3Aserve',
Expand Down

0 comments on commit 29464eb

Please sign in to comment.