- Notifications
You must be signed in to change notification settings - Fork12
Types and mocks for building a tested Typescript Cloudflare Worker, generates three NPM packages
License
udacity/cloudflare-typescript-workers
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Retired: as of 2022, Cloudflare's official types package@cloudflare/workers-types
is more up-to-date and better supported.
Cloudflare Workers allow you to run JavaScript on Cloudflare'sedge servers around the world. You can modify your site’s HTTP requests andresponses, make parallel requests, or generate responses from the edge. Thisproject develops, tests, and deploys NPM packages supporting the development ofCloudflare Workers using TypeScript.
Get started on your own TypeScript Cloudflare Worker using theTemplate.
types-cloudflare-worker
- Complete types for allpublic features provided by Cloudflare Workers. Supports theRequest.cf
object, the Cache API and KV API.cloudflare-worker-mock
- Wrapsservice-worker-mock
for name consistency, developer experience and toprovide a simple mockable Cache API and KV API implementation.@udacity/types-service-worker-mock
- Incompletetypes for thepinterest/service-worker-mock
to support Cloudflare WorkerTesting. May be pushed to the NPM@types
project in the future, but needsadditional work before that is reasonable.
TheCloudflare Worker API implements a subset of theService Worker API specification, therefore Service WorkerTypeScript types are a useful starting point.
A small example of a strictly typed worker:
importCloudflareWorkerGlobalScopefrom'types-cloudflare-worker';declarevarself:CloudflareWorkerGlobalScope;exportclassWorker{publicasynchandle(event:FetchEvent){constoriginResponse=fetch(event.request,{cf:{minify:{html:true,},},});returnoriginResponse;}}self.addEventListener('fetch',(event:FetchEvent)=>{constworker=newWorker();event.respondWith(worker.handle(event));});
Start with theStarter Template or run the followingcommands:
npm init# Add TypeScriptnpm i -D \ typescript \ @types/node# Setup the Cloudflare Worker Types and Mocknpm i -D \ @udacity/types-service-worker-mock \ types-cloudflare-worker \ cloudflare-worker-mock# Init TypeScripttsc --init
Set the TypeScript compiler options intsconfig.json
:
"compilerOptions":{/* https://developers.cloudflare.com/workers/reference/ *//* Cloudflare Workers use the V8 JavaScript engine from Google Chrome. The * Workers runtime is updated at least once a week, to at least the version * that is currently used by Chrome’s stable release. This means you can * safely use latest JavaScript features, with no need for "transpilers". */"target":"ESNext",/* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */"module":"commonjs",/* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */"lib":["esnext","webworker"],/* Specify library files to be included in the compilation. *//* Recommend enabling all Strict Type-Checking Options and Additional Checks */}
There are two implementations of the same test worker in this repository.
This is the demo worker. It uses the published packages. Reference it as anexample implementation and starting point for your project.
cd demonpm ijestnpm run build
This is the development worker. It uses the local packages.
npm ijestnpm run build
npm inpm run test-allscripts/publish.sh$VERSION$OTP# New version and 2FA
Brad Erickson (@13rac1)
Licensed under the Apache License, Version 2.0.
© 2019 Udacity, Inc.
Content derived from Cloudflare Developer Documentation. © 2019 Cloudflare, Inc.
About
Types and mocks for building a tested Typescript Cloudflare Worker, generates three NPM packages
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors7
Uh oh!
There was an error while loading.Please reload this page.