Gathering detailed insights and metrics for @nurupo/vite-plugin-cloudflare-functions
Gathering detailed insights and metrics for @nurupo/vite-plugin-cloudflare-functions
Make Cloudflare Pages Functions works with Vite friendly
npm install @nurupo/vite-plugin-cloudflare-functions
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (93.89%)
Vue (4.69%)
HTML (1.41%)
Total Downloads
324
Last Day
2
Last Week
4
Last Month
23
Last Year
324
58 Stars
1,063 Commits
8 Forks
3 Watching
3 Branches
4 Contributors
Latest Version
0.8.1
Package Id
@nurupo/vite-plugin-cloudflare-functions@0.8.1
Unpacked Size
62.62 kB
Size
14.12 kB
File Count
19
NPM Version
10.9.0
Node Version
20.10.0
Publised On
13 Nov 2024
Cumulative downloads
Total Downloads
Last day
0%
2
Compared to previous day
Last week
-33.3%
4
Compared to previous week
Last month
130%
23
Compared to previous month
Last year
0%
324
Compared to previous year
6
Make Cloudflare Pages Functions works with Vite friendly.
When should you use this plugin?
If you have used some meta SSR framework like nuxt, there is no need to use this plugin. But if you want to add some API endpoints to your SPA, just use it.
This plugin provides some simple utilities to help you develop a SPA with serverless API endpoints powered by Cloudflare Pages Functions.
1npm i -D vite-plugin-cloudflare-functions
1// vite.config.ts 2 3import { defineConfig } from 'vite'; 4 5import CloudflarePagesFunctions from 'vite-plugin-cloudflare-functions'; 6 7export default defineConfig({ 8 plugins: [ 9 CloudflarePagesFunctions() 10 ] 11});
Just write pages functions as usual, but you can use the following utility functions to make auto-generation and IDE type inference work.
makePagesFunction
makeRawPagesFunction
makeResponse
: create a Response using your JSON objectmakeRawResponse
: same with new Response(...) but wrapped with a generic type used for type inference1// /api/[msg].ts 2 3import { 4 makeRawPagesFunction, 5 makePagesFunction, 6 makeResponse 7} from 'vite-plugin-cloudflare-functions/worker'; 8 9export const onRequestGet = makePagesFunction(({ params }) => ({ 10 status: 'OK', 11 data: 'Hello, ' + params.msg + '!' 12})); 13 14export const onRequestPost = makeRawPagesFunction(({ params }) => 15 makeResponse({ 16 status: 'OK', 17 data: 'Post ' + params.msg + ' OK!' 18 }) 19);
For example, you have set the environment variable PASS
(you can config it in the plugin option, see Configuration).
1// cloudflare.d.ts 2 3/// <reference types="@cloudflare/workers-types" /> 4/// <reference types="vite-plugin-cloudflare-functions/types" /> 5 6import 'vite-plugin-cloudflare-functions/worker'; 7 8declare module 'vite-plugin-cloudflare-functions/worker' { 9 interface PagesFunctionEnv { 10 PASS: string; 11 } 12 13 interface PagesFunctionData {} 14}
Then you can find the parameter env
has corresponding type declarations.
1// /api/index.ts 2 3import { makePagesFunction } from 'vite-plugin-cloudflare-functions/worker'; 4 5export const onRequestGet = makePagesFunction(({ env }) => ({ 6 pass: env.PASS 7}));
Just write you client code as usual, but for we generate the API endpoint response body type declarations automatically, so that with the provided client useFunctions
(powered by axios), your IDE will provide smarter IntelliSense.
1// /main.ts 2import { useFunctions } from 'vite-plugin-cloudflare-functions/client'; 3 4const client = useFunctions(); 5 6client.get('/api/world').then((resp) => { 7 // The type of resp is { status: string, data: string } 8});
Full example is here.
1// vite.config.ts
2
3import { defineConfig } from 'vite';
4
5import CloudflarePagesFunctions from 'vite-plugin-cloudflare-functions';
6
7export default defineConfig({
8 plugins: [
9 CloudflarePagesFunctions({
10 // Cloudflare Functions root directory
11 root: './functions',
12 // Copy the functions directory to outDir or do nothing
13 outDir: undefined,
14 // Generate API type declarations
15 dts: './cloudflare.d.ts',
16 // Wrangler configuration
17 wrangler: {
18 // Wrangler dev server port
19 port: 8788,
20 // Enable wrangler log
21 log: true,
22 // Bind variable/secret
23 binding: {
24 KEY: 'VALUE'
25 },
26 // Bind KV namespace
27 kv: [
28 'NAMESPACE'
29 ],
30 // Bind D1 database
31 d1: [
32 'D1'
33 ],
34 // Bind Durable Object
35 do: {
36 DO: 'DO'
37 },
38 // Bind R2 bucket
39 r2: [
40 'BUCKET'
41 ]
42 }
43 })
44 ]
45});
Note
This plugin starts the wrangler pages dev server under the hood. The configuration field
binding
,kv
,do
,d1
,r2
are passed to run the commandwrangler pages dev
to start pages dev server. You can find more information about this command at Commands - Cloudflare Worker docs.The generated command of the above example is
wrangler pages dev --local --ip localhost --port 8788 --proxy <generated by vite dev server> --binding KEY=VALUE --kv NAMESPACE --d1 D1 --do DO=DO --r2 BUCKET
.
MIT License © 2022 XLor
No vulnerabilities found.
No security vulnerabilities found.