Skip to content

Commit

Permalink
add script types and a simple type guard (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
flashdesignory authored Oct 11, 2023
1 parent ef38c23 commit 04e7407
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 42 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
export { GoogleAnalytics } from './third-parties/google-analytics';
export { GoogleMapsEmbed } from './third-parties/google-maps-embed';
export { YouTubeEmbed } from './third-parties/youtube-embed';

export * from './types';
2 changes: 1 addition & 1 deletion src/third-parties/google-analytics/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"action": "append"
},
{
"code": "window.dataLayer=window.dataLayer||[];window.gtag=function gtag(){window.dataLayer.push(arguments);};gtag('js',new Date());gtag('config','${args.id}')",
"code": "window.dataLayer=window.dataLayer||[];window.gtag=function gtag(){window.dataLayer.push(arguments);};gtag('js',new Date());gtag('config','{{id}}')",
"strategy": "worker",
"location": "head",
"action": "append"
Expand Down
39 changes: 2 additions & 37 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,2 @@
type ScriptStrategy = 'server' | 'client' | 'idle' | 'worker';
type ScriptLocation = 'head' | 'body';
type ScriptAction = 'append' | 'prepend';
export type SrcVal = {
url: string;
slugParam?: string;
params?: Array<string>;
};

export type AttributeVal = string | null | SrcVal | boolean | undefined;

export type HtmlAttributes = {
src?: SrcVal;
[key: string]: AttributeVal;
};

export interface Data {
id: string;
description: string;
website?: string;
html?: {
element: string;
attributes: HtmlAttributes;
};
stylesheets?: Array<string>;
scripts?: Array<{
url: string;
params?: Array<string>;
strategy: ScriptStrategy;
location: ScriptLocation;
action: ScriptAction;
}>;
}

export interface Inputs {
[key: string]: any;
}
export * from './type-declarations';
export * from './type-guards';
49 changes: 49 additions & 0 deletions src/types/type-declarations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
type ScriptStrategy = 'server' | 'client' | 'idle' | 'worker';
type ScriptLocation = 'head' | 'body';
type ScriptAction = 'append' | 'prepend';
export type SrcVal = {
url: string;
slugParam?: string;
params?: Array<string>;
};

export type AttributeVal = string | null | SrcVal | boolean | undefined;

export type HtmlAttributes = {
src?: SrcVal;
[key: string]: AttributeVal;
};

type ScriptBase = {
params?: Array<string>;
strategy: ScriptStrategy;
location: ScriptLocation;
action: ScriptAction;
};

export type ExternalScript = ScriptBase & {
url: string;
};

export type CodeBlock = ScriptBase & {
code: string;
};

export type Script = ExternalScript | CodeBlock;
export type Scripts = Script[];

export interface Data {
id: string;
description: string;
website?: string;
html?: {
element: string;
attributes: HtmlAttributes;
};
stylesheets?: Array<string>;
scripts?: Scripts;
}

export interface Inputs {
[key: string]: any;
}
5 changes: 5 additions & 0 deletions src/types/type-guards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Script, ExternalScript } from '.';

export function isExternalScript(script: Script): script is ExternalScript {
return (script as ExternalScript).url !== undefined;
}
4 changes: 2 additions & 2 deletions src/utils/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { formatUrl, createHtml, formatData } from '.';
import type { Data } from '../types';
import type { Data, ExternalScript } from '../types';

describe('Utils', () => {
describe('formatUrl', () => {
Expand Down Expand Up @@ -143,7 +143,7 @@ describe('Utils', () => {
expect(result.html).toEqual('<iframe loading="lazy"></iframe>');
expect(result.scripts).not.toEqual(null);
expect(result.scripts!.length).toEqual(1);
expect(result.scripts![0].url).toEqual(
expect((result.scripts![0] as ExternalScript).url).toEqual(
'https://www.example.com/?id=userDefinedId',
);
});
Expand Down
14 changes: 12 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Data, Inputs, AttributeVal, HtmlAttributes } from '../types';
import { isExternalScript } from '../types';

function filterArgs(
args: Inputs,
Expand Down Expand Up @@ -38,6 +39,12 @@ export function formatUrl(
return newUrl.toString();
}

export function formatCode(code: string, args?: Inputs) {
return code.replace(/{{(.*?)}}/g, (match) => {
return args?.[match.split(/{{|}}/).filter(Boolean)[0]];
});
}

// Construct HTML element and include all default attributes and user-inputted attributes
export function createHtml(
element: string,
Expand Down Expand Up @@ -127,12 +134,15 @@ export function formatData(data: Data, args: Inputs) {
// Pass any required query params with user values for relevant scripts
scripts: data.scripts
? data.scripts.map((script) => {
return script.url
return isExternalScript(script)
? {
...script,
url: formatUrl(script.url, script.params, scriptUrlParamInputs),
}
: script;
: {
...script,
code: formatCode(script.code, scriptUrlParamInputs),
};
})
: null,
};
Expand Down

0 comments on commit 04e7407

Please sign in to comment.