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 TypeScript support to template-registry #1

Closed
wants to merge 24 commits into from
Closed

Conversation

ispivey
Copy link
Owner

@ispivey ispivey commented May 9, 2020

The goal of this change is to eventually allow us to write snippets in TypeScript, transpile them to JavaScript, and surface both versions in the Template Gallery. By treating the TypeScript snippets as the source-of-truth, we ensure they stay in sync.

This also gives us a good way to catch potential bugs in the JavaScript versions of the snippets!

In the short term, this specific change should effectively be a no-op for the Template Gallery — the API only surfaces the JS files. If folks think this is a good idea, we can do further work to expose the TypeScript snippets in the docs.

Eventually, we should consider removing the transpiled JavaScript snippets from source control entirely. For now, it seems more helpful to keep them in source control so reviewers can see the changes that will result from replacing the JavaScript snippets with TypeScript snippets and transpiling back to JS.

Please consider reviewing commit-by-commit, as commits are small and related in scope.

This change does several things:

  • Adds an npm script command to run prettier, which is in line with the repo standards, and uses the existing .prettierrc.
  • Runs prettier and commits the small number of resulting changes
  • Adds a templates/typescript folder, as well as a transpile command that uses tsc to transpile any TypeScript files in said folder into the templates/javascript folder.
  • Translates many JavaScript snippets into TypeScript.

After translating each JS snippet into TS, I ran npm run transpile && npm run format. If a commit only contains a new TypeScript snippet, that means the transpiled JavaScript version was identical to the JavaScript snippet already in source control.

This change does not seek to meaningfully change or improve the snippets themselves, other than to fix bugs or typos in comments.

As a first example, this change creates typescript/alter_headers.ts.
Then, I ran: `npm run transpile && npm run format`.

tsc overwrote javascript/alter_headers.js, and prettier formatted it
to match the repo's existing style conventions, resulting in no
actual diff to alter_headers.js -- but the code is now generated from
TypeScript!
Noteworthy changes:
* Response.Body.json() has a return value of type Any; we should explicitly
  stringify it before possibly passing it as the first argument to the
  Response constructor.
* The Response constructor expects a string as its first argument. The
  runtime does attempt to stringify arguments passed to it, but we should
  explicitly join the list of strings in `results` before passing it as an
  argument to the constructor.
@@ -1,7 +1,8 @@
{
"arrowParens": "avoid",
Copy link
Owner Author

Choose a reason for hiding this comment

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

I added this to conform with the existing snippet conventions in this repo. The vast majority don’t use parens around arguments to arrow functions, so this was least disruptive.

"singleQuote": true,
"semi": false,
"trailingComma": "all",
"tabWidth": 2,
"printWidth": 80
"tabWidth": 2
Copy link
Owner Author

Choose a reason for hiding this comment

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

This was part of the motivation for including prettier. By default, tsc transpilation introduces 4-space indentation, which would have been disruptive and made it harder to identify changes to the JS snippets.

@ispivey
Copy link
Owner Author

ispivey commented May 10, 2020

bulk_origin_proxies depends on cloudflare/workers-types#18

@ispivey
Copy link
Owner Author

ispivey commented May 10, 2020

cache_api depends on ispivey/workers-types#1 to add type support for caches.default ... or cloudflare/workers-types#12

@ispivey
Copy link
Owner Author

ispivey commented May 16, 2020

Going to re-open as a PR against upstream.

@ispivey ispivey closed this May 16, 2020
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.

1 participant