Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/koistya/cloud-functions-routing
Google Cloud Functions example using Node.js, TypeScript, folder-based routing, hot module replacement (HMR), and unit tests.
https://github.com/koistya/cloud-functions-routing
cloud-functions express expressjs gcp google-cloud next nextjs router routing typescript unit-testing vite vitest
Last synced: 11 days ago
JSON representation
Google Cloud Functions example using Node.js, TypeScript, folder-based routing, hot module replacement (HMR), and unit tests.
- Host: GitHub
- URL: https://github.com/koistya/cloud-functions-routing
- Owner: koistya
- License: mit
- Created: 2023-10-08T21:13:39.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-10-09T16:53:59.000Z (about 1 year ago)
- Last Synced: 2024-10-25T18:14:31.826Z (about 2 months ago)
- Topics: cloud-functions, express, expressjs, gcp, google-cloud, next, nextjs, router, routing, typescript, unit-testing, vite, vitest
- Language: TypeScript
- Homepage:
- Size: 5.86 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Google Cloud Functions Routing
This example demonstrates how to implement a simple folder-based routing for Google Cloud Functions including:
- [x] Development server with live-reload (HMR)
- [x] Code bundling with Vite
- [x] Unit testing with Vitest## Tech Stack
Node.js v18 or newer, TypeScript, Vite, Vitest, `path-to-regexp`.
## Getting Started
```bash
$ git clone https://github.com/koistya/cloud-functions-routing
$ cd ./cloud-functions-routing
$ corepack enable # Ensure that Yarn is installed
$ yarn install # Install dependencies
$ yarn start # Launch Node.js app with "live-reload"
$ yarn build # Build the app for production
$ yarn test # Run unit tests
```To add a new route create a new `.ts` file under `./routes` folder, e.g. `./routes/posts/[id].ts`, with the following signature:
```ts
import { HttpFunction } from "@google-cloud/functions-framework";/**
* Fetches a post by ID.
*
* @example GET /api/posts/1
*/
export const GET: HttpFunction = async (req, res) => {
const url = `https://jsonplaceholder.typicode.com/posts/${req.params.id}`;
const fetchRes = await fetch(url);
const post = await fetchRes.json();
res.send(post);
};
```To create a unit test for it, create a new `.test.ts` file next to it, e.g. `./routes/posts/[id].test.ts`. For example:
```ts
import { getTestServer } from "@google-cloud/functions-framework/testing";
import supertest from "supertest";
import { expect, test } from "vitest";
import { functionName } from "../../index";test("GET /api/posts/1", async () => {
const api = getTestServer(functionName);
const res = await supertest(api)
.get("/api/posts/1")
.set("Accept", "application/json");expect({
statusCode: res.statusCode,
body: res.body,
}).toEqual({
statusCode: 200,
body: {
id: 1,
userId: 1,
title:
"sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
body:
"quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit " +
"molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto",
},
});
});
```## References
- https://vitejs.dev/guide/features.html#glob-import
- https://github.com/pillarjs/path-to-regexp#readme## Backers
## License
Copyright © 2023-present Konstantin Tarkus. This source code is licensed under the MIT license found in the [LICENSE](https://github.com/koistya/cloud-functions-routing/blob/main/LICENSE) file.