Skip to content
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 support for widget injection in connect command #238

Merged
merged 10 commits into from
Oct 2, 2023

Conversation

Yshayy
Copy link
Contributor

@Yshayy Yshayy commented Oct 2, 2023

This is a

  • Bug fix
  • Feature
  • Doc improvement

This PR update the format of script labels to preevy.inject_script.(script_id).(script_prop)=value + tests.
It also adds support for using Preevy widget in connect command

@Yshayy Yshayy requested a review from royra October 2, 2023 08:13
packages/common/src/compose-tunnel-agent.ts Outdated Show resolved Hide resolved
packages/common/src/compose-tunnel-agent.ts Show resolved Hide resolved
for (const [label, value] of Object.entries(labels)) {
const match = label.match(re)?.groups
if (match) {
set(sections, [match.id, camelCase(match.key)], value)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think it would be clearer if there was a function whose purpose is to convert labels to an array of Record<string, string>. this function's responsibility is more strictly defined IMO, with concepts like key case not included.

then have a separate function which maps the record objects to a ScriptInjection explicitly:

const stringRecordToScriptInjection = (o: Record<string, string>) => ({
  src: o.src, // throw on missing
  defer: o.defer && o.defer === 'true',
  ...
})

Bonus - you can use Zod to verify and convert the schema including coercing to boolean.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, it'd be parseScriptInjection = recordParser(def)
// recordParser = (def) => (Record<string,string >)=> T
// where def can be something declarative as Zod)

But I think it's a bit pre-mature to extract it and Zod is a bit tricky since it translate 'false' to true which might be correct in js, but we are in the context of configuration.

)
return Object.values(scripts)
.map(parseScriptInjection)
.filter((x): x is ScriptInjection => !(x instanceof Error))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably worth logging the error

packages/core/src/compose/script-injection.ts Show resolved Hide resolved

export const scriptInjector = (id : string, script: ScriptInjection) => {
const injectScript = (model:ComposeModel, service:string) => addScript(model, service, { id, ...script })
const injectAll = (_serviceName:string, _def: ComposeService) => true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

args can be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we talking about injectAll? The args are there for inferring the signature type, they are prefixed with _ as they are not in use

packages/core/src/compose/script-injection.ts Outdated Show resolved Hide resolved
const injectScript = (model:ComposeModel, service:string) => addScript(model, service, { id, ...script })
const injectAll = (_serviceName:string, _def: ComposeService) => true
return {
inject: (model: ComposeModel, serviceFilter = injectAll) =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can return a function instead of an object with a single key. or even simpler IMO - remove the nested func and have a single function with combined args.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created it initially as a single function, the reason for creating the nested function is for currying the service (id, params) from the injection context (model, filter), otherwise it doesn't provide much value compared to using addScript (which is used externally) directly.

The reason for returning an object and not a single function was for better communicating the action.

packages/core/src/compose/script-injection.ts Show resolved Hide resolved
@Yshayy Yshayy merged commit 1db2036 into main Oct 2, 2023
8 checks passed
@royra royra deleted the script-injection-updates branch October 2, 2023 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants