Comments (11)
Just had a discussion with @toinbis on Discord who brought me up to speed. The issue breaks down into two pieces:
- This repo is a bit out of date, there are a couple of other issues open with missing types etc. It should be more actively maintained to better encourage writing Workers in TS.
- A new repo should be spun up that gives an example of well-written Workers: properly typed, tested etc. That repo would import this one (demonstrating how to set up the TS config correctly) as well as have unit tests (demonstrating how to setup a testing mock/scaffold).
I'll probably pick this up when I next get some spare cycles, so if anyone has anything else to add/ask for pop it here in the next couple of days and I'll look at it at the same time.
from workers-types.
I've just released edge-mock
which should accomplish what's requested here.
A few advantages of edge-mock
over service-worker-mock
:
service-workers
the project whichservice-worker-mock
is a part of is no longer maintainededge-mock
provides support for KV store mocking out-of-the-boxedge-mock
properly supportsArrayBuffer
andReadableStream
whichservice-worker-mock
does notedge-mock
has close to 100% test coverage and is written in typescript- All the implementations provided by
edge-mock
are fully compliant with the types they implement in typescript FormData
is properly supported byedge-mock
, both parsing and serialising
I've created a PR to suggest using this in the typescript template cloudflare/worker-typescript-template#45
I've also updated worker-typescript-template to use jest instead of mocha/chai
.
I still need to write comprehensive docs for edge-mock
(help very welcome) and perhaps in future add support for more exotic CF types like durable objects, html rewrite, scheduled events and image transforms (I didn't even know about this until reading index.d.ts
here). But the basics all complete and ready to use. Feedback welcome.
from workers-types.
Hi @samuelcolvin ,
thanks a lot!! I am actually following you on github and have noticed you progressing on edge-mock
over the last weekend(alongside https://github.com/samuelcolvin/edgerender) and was super happy to see you tackling both problems!
I'll share more feedback when I try edge-mock
in one of my projects, hope that might happen in the following 2-3 weeks.
As of now - I could only say that durable objects, html rewrites and scheduled events seems to me not THAT exotic and would be great to have those covered as well. Hopefully community steps in to help on those.
Not sure if it's best to leave this issue open for now, or close. Will leave that for CF team to decide.
And as of now I'am back to writting some pydantic
models that are used to generate and validate json config file that is later parsed with cloudflare workers (everything's connected, haha :) For those who don't, @samuelcolvin is author of super popular and super wonderful data validation library pydantic
- https://pydantic-docs.helpmanual.io , it's awesome to see him contributing to CF Workers ecosystem as well! Samuel - thanks again!
from workers-types.
Anyone at Cloudflare who can follow up on this? Would be awesome to get official mocking support.
from workers-types.
@toinbis thanks for the thoughtful comment.
I also used https://github.com/cloudflare/worker-typescript-template but found it a bit dated:
- npm warned of security vulnerabilities in some of the pinned dependencies
- I've had troubles with chai/mocha for testing. I'm lacking some domain expertise because including a worker kv namespace yields test failures.
import { KVNamespace } from "@cloudflare/workers-types"
gives red squigglies in vscode because@cloudflare/worker-types/index.d.ts
is not a module.- the last commit was 9 months ago, and given the pace at which the ecosystem moves, this feels a bit like it's not really maintained.
I'm happy to be wrong about all/most of the above points, so consider my comment as βplease help a beginner" more than a complaint. π
> tsc --project ./test
src/handler.ts:62:34 - error TS2304: Cannot find name 'FIRST_KV_NAMESPACE'.
62 const value: string = (await FIRST_KV_NAMESPACE.get(domain)) || ""
~~~~~~~~~~~~~~~~~~
Found 1 error.
from workers-types.
Hey there, I've just joined the Cloudflare Workers team and would be interested in hearing more about what's needed here. @toinbis @hmnd if you're still interested in this topic maybe the first thing to try would be to join the Workers Discord that we recently opened up (https://blog.cloudflare.com/meet-the-workers-team-over-discord/) which will let you @-notify or DM me (geelen) and we can chat?
from workers-types.
Thanks, @geelen - missed the discord announcement. Have joined and DM'ed you. Thanks a lot for following-through!
from workers-types.
@geelen That sums up my wishes for this repo and Workers in TS quite well, too. I'll let you know if anything else comes to mind.
from workers-types.
I wholeheartedly agree that having a canonical typescript example, with tests and modern tooling for a Cloudflare worker would go along way towards increasing adoption.
I am new to the javascript/typescript/npm world, and I'm having a lot of troubles finding a modern typescript cf worker example.
@geelen is this still that's on your backlog by any chance?
from workers-types.
Hi @pims, I've been using https://github.com/cloudflare/worker-typescript-template as a starter quite successfully.
Also bear in mind that since last 4 days https://github.com/cloudflare/wrangler/releases/tag/v1.16.0 there are two worker formats - service-worker
(good old) and modules
(in beta). Modules format workers look like https://github.com/cloudflare/workers-chat-demo/blob/master/src/chat.mjs . You probably want to stick to default for now and the repo I provided above is a great place to start. Other than that - https://blog.cloudflare.com/meet-the-workers-team-over-discord/ workers Discord is highly recommended to hang out and ask questions and get support.
@geelen - having that said, yesterday was again setting jest with typescript and mocking support (again, haha), and also came to revisit this ticket to add +1. I can imagine with some many releases on the way there are higher priorities, but I do believe it's really worth having this ticket in the backlock ;) tnx!!
from workers-types.
The recommended and supported solution is to now use miniflare.dev, which is a full simulation of the Workers environment. Closing this issue, but let me know if that isn't sufficient.
from workers-types.
Related Issues (20)
- Missing properties on IncomingRequestCfPropertiesBotManagement HOT 1
- Incorrect D1 types HOT 2
- RequestInit is missing some properties like 'cache' HOT 3
- Generate types from `workerd` RTTI HOT 1
- RFC: Exportable types HOT 7
- Fix `request.cf` types
- Generate exportable version of `workers-types` HOT 1
- add JSDocs documentation in types
- Move overrides to `workerd` C++ source code HOT 1
- Include C++ parameter names in types generated from JSG RTTI HOT 2
- Get type hints on incoming Request object HOT 4
- IncomingRequestCfPropertiesGeographicInformation makes geo property access an error (regression from 3.17.0) HOT 7
- HtmlRewriter ElementHandler methods can only accept strings (not general Content) HOT 1
- (3.18.0) IncomingRequestCfPropertiesGeographicInformation is the wrong type HOT 2
- Class 'Request<ParamKeyType, Data>' incorrectly extends base class 'Body' HOT 1
- `new Request` bad type HOT 8
- addEventListener has a broken type definition HOT 1
- NPM Release @cloudflare/worker-types 4.20221111.0 HOT 2
- Add Symbol.iterator to WebScoketPair HOT 1
- What is Fetcher for? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from workers-types.