Giter Club home page Giter Club logo

kodadot-workers's Introduction

workers

Implementation of Cloudflare Workers

image

How to publish worker

Contributing

Create a new worker

This is a guide to create a new typescript worker

  1. When you create a new worker, you need to add a new directory in the root.

Usually the name of the directory is the same as the name of the worker. The name is usually stated in the issue

npx wrangler init profile
  1. Install deps

For most of the typescript workers, you need to install hono

npm install hono --save

For the better developer experience please also install prettier

npm install prettier --save-dev

If the workers requires using D1

Install kysely and kysely-d1 as the dependecies

npm install kysely kysely-d1 --save

Install better-sqlite3 and kysely-codegen as the dev dependencies

npm install better-sqlite3 kysely-codegen --save-dev

To use kysely-codegen to generate the typescript types, you need to add the following script in package.json

{
  "scripts": {
    "codegen": "kysely-codegen --dialect sqlite --out-file src/utils/types.ts",
  }
}

โš ๏ธ to use kysely-codegen, you need to have a .env file.

DATABASE_URL=/absolute/path/to/the/working/directory/workers/<worker-name>/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/<uuid>.sqlite

Create a database file with schema called schema.sql.

If the workers requires using fetching data

Install ofetch as the dependencies

npm install ofetch --save

To create new service, you need to create a new file in src/utils directory.

import { $fetch, FetchError } from 'ohmyfetch'

const BASE_URL = 'http://localhost:8787'

const api = $fetch.create({
  baseURL: BASE_URL,
})

If the workers requires using data from KodaDot

Install @kodadot/uniquery as the dependencies

npm install @kodadot/uniquery --save

Usage can be found in kodadot/uniquery repository.

  1. Code structure

As there is not much restriction on the code structure, you can refer to the existing workers.

make sure that dependencies in package.json are before dev dependencies

If the workers using middleware

Put the middleware in src/middleware directory.

import { Context } from 'hono'
import { HonoEnv } from '../utils/constants'

export const sampleMiddleware = async (c: Context<HonoEnv, string>, next: Function) => {
  // do something
  await next()
}

If the workers using more than one route

Put the routes in src/routes directory.

import { Hono } from 'hono'
import { HonoEnv } from '../utils/constants'


const app = new Hono<HonoEnv>()

app.get('/', async (c) => {
  return c.json({ message: 'Hello World' })
})

export { app as sameNameAsTheFile }

then in src/index.ts

app.route('/complex-route', sameNameAsTheFile)
  1. Making a PR

After you have done the implementation, you can make a PR to this repository. Please make sure that you have tested the worker locally before making a PR. Please use prettier to format the code before making a PR.

kodadot-workers's People

Contributors

preschian avatar vikiival avatar roileo avatar hassnian avatar alko89 avatar yangwao avatar jarsen136 avatar daiagi avatar complexlity avatar hamidovz avatar

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.