-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add script types and a simple type guard #27
Changes from 6 commits
3024231
4427da4
ddb1aef
676e5af
0738f81
e43c91c
0813c45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,8 +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}')", | ||
"strategy": "worker", | ||
"code": "window.dataLayer=window.dataLayer||[];window.gtag=function gtag(){window.dataLayer.push(arguments);};gtag('js',new Date());gtag('config','{{id}}')", "strategy": "worker", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Identation I thought I already set up Prettier in this package, but maybe I need to take another look 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤦♂️ - fixed |
||
"location": "head", | ||
"action": "append" | ||
} | ||
|
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'; |
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; | ||
} |
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; | ||
} |
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, | ||
|
@@ -38,6 +39,12 @@ export function formatUrl( | |
return newUrl.toString(); | ||
} | ||
|
||
export function formatCode(code: string, args?: Inputs) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this function replaces the template tags with args passed in |
||
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, | ||
|
@@ -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, | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a way to get the types exported...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My TS skills are not as good as I'd like them to be lol
Is this how folks generally export types? Do you know if packages usually includes a types file as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's maybe a more "proper" way, this just made it work for me.
@janicklas-ralph - what did you have in mind?
would be nice maybe to be able to import them from "third-party-capital/types" instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah tbh I'm not sure, but we can figure that out in a future PR :)