GithubHelp home page GithubHelp logo

Comments (11)

geelen avatar geelen commented on September 17, 2024 2

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.

samuelcolvin avatar samuelcolvin commented on September 17, 2024 2

I've just released edge-mock which should accomplish what's requested here.

A few advantages of edge-mock over service-worker-mock:

  1. service-workers the project which service-worker-mock is a part of is no longer maintained
  2. edge-mock provides support for KV store mocking out-of-the-box
  3. edge-mock properly supports ArrayBuffer and ReadableStream which service-worker-mock does not
  4. edge-mock has close to 100% test coverage and is written in typescript
  5. All the implementations provided by edge-mock are fully compliant with the types they implement in typescript
  6. FormData is properly supported by edge-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.

toinbis avatar toinbis commented on September 17, 2024 2

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.

hmnd avatar hmnd commented on September 17, 2024 1

Anyone at Cloudflare who can follow up on this? Would be awesome to get official mocking support.

from workers-types.

pims avatar pims commented on September 17, 2024 1

@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.

geelen avatar geelen commented on September 17, 2024

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.

toinbis avatar toinbis commented on September 17, 2024

Thanks, @geelen - missed the discord announcement. Have joined and DM'ed you. Thanks a lot for following-through!

from workers-types.

hmnd avatar hmnd commented on September 17, 2024

@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.

pims avatar pims commented on September 17, 2024

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.

toinbis avatar toinbis commented on September 17, 2024

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.

threepointone avatar threepointone commented on September 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.