Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: enhance documentation: add various example #58

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@ examples/handlers/route.ts
examples/handlers/route-map.ts
.eslintcache
TODO.md
git-short.sh
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Why `fast-maker`?
- [Route options](#route-options)
- [Route handler](#route-handler)
- [Example using fastify.js](#example-using-fastifyjs)
- [Examples](#examples)
- [Relate To](#relate-to)
- [Roadmaps](#roadmaps)
- [License](#license)
Expand Down Expand Up @@ -235,11 +236,19 @@ export async function handler(
}
```

You have to `named export` and variable name must be a `handler`. Also you can use arrow function and you can use any name under TypeScript function name rule, as well as type arguments perfectly applied on route configuration
You have to `named export` and variable name must be a `handler`. Also you can use arrow function, as well as type arguments perfectly applied on route configuration.

## Example using fastify.js

A complete example of using `fast-maker` can be found at [Ma-eum](https://github.com/maeumjs/maeum-pet-store).
A complete example of using `fast-maker` can be found at [Ma-eum](https://github.com/maeumjs/maeum-pet-store) and [examples](https://github.com/imjuni/fast-maker/tree/master/examples)

### Examples

- [vanilla](examples/handlers/justice/%5Bdc-league%5D/hello/get.ts)
- [variable map](examples/handlers/avengers/heroes/%5Bid%5D/%5B%24time%5D/post.ts)
- [multiple methods](examples/handlers/avengers/heroes/get.ts)
- [multiple variable](examples/handlers/justice/[kind]-[id]/get.ts)
- [function option](examples/handlers/po-ke/world/get.ts)

## Relate To

Expand Down
Binary file modified assets/route-component-architecture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions examples/handlers/avengers/heroes/[[id]]/get.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Example of the nullable variable
*
* A nullable variable uses double square brackets.
*
* [[id]] replace to `:id?`
*/
import type { HTTPMethods } from 'fastify';

export const methods: HTTPMethods = 'SEARCH';
Expand Down
7 changes: 7 additions & 0 deletions examples/handlers/avengers/heroes/[id]/[$time]/post.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/**
* Example of the route path map.
*
* Regular expressions, wildcards, etc. cannot be directory created, so use variable maps.
*/
import type { FastifyRequest } from 'fastify';
import type { IAbility } from '../../../../../interface/IAbility';

// Route path map
// [$time] replace to `:hour(^\\d{2})h:minute(^\\d{2})m`
export const map: Map<string, string> = new Map<string, string>([['time', ':hour(^\\d{2})h:minute(^\\d{2})m']]);

export async function handler(req: FastifyRequest<{ Body: IAbility }>) {
Expand Down
9 changes: 8 additions & 1 deletion examples/handlers/avengers/heroes/get.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/**
* Example of the extra methods
*
* To apply more than one method to a single handler, use the `methods` variable.
*/
import type { HTTPMethods } from 'fastify';

export const methods: HTTPMethods = 'SEARCH';
// methods variable
// methods variable replace to `fastify.route({ methods: ['get', 'search', 'options'] })`
export const methods: HTTPMethods[] = ['SEARCH', 'OPTIONS'];

export async function handler() {
return 'hello';
Expand Down
6 changes: 3 additions & 3 deletions examples/handlers/justice/[dc-league]/hello/get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FastifyInstance, FastifyReply, FastifyRequest, RouteShorthandOptions } from 'fastify';
import type { FastifyReply, FastifyRequest, RouteShorthandOptions } from 'fastify';
import type { Server } from 'http';
import type { IReqPokeHello } from '../../../interfaces/IReqPokeHello';
import schema from '../../../interfaces/JSC_IReqPokeHello';
Expand All @@ -7,12 +7,12 @@ export const map: string = 'test';

export const methods: number = 1;

export const option: (fastify: FastifyInstance) => RouteShorthandOptions = () => ({
export const option: RouteShorthandOptions = {
schema: {
querystring: schema.properties?.Querystring,
body: schema.properties?.Body,
},
});
};

export async function handler(req: FastifyRequest<IReqPokeHello, Server>, _reply: FastifyReply) {
console.debug(req.query);
Expand Down
5 changes: 5 additions & 0 deletions examples/handlers/justice/[kind]-[id]/get.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Example of the multiple variable in single route path element
*
* justice/[kind]-[id] replace to `justice/:kind-:id`
*/
import type { FastifyInstance, FastifyReply, FastifyRequest, RouteShorthandOptions } from 'fastify';
import type { Server } from 'http';
import type { IReqPokeHelloMultiParam } from '../../interfaces/IReqPokeHelloMultiParam';
Expand Down
10 changes: 10 additions & 0 deletions examples/handlers/po-ke/world/get.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Example of the function option
*
* If you need access to the fastify instance to use various hooks, including the preHandler,
* you can use the function option to get the fastify instance passed to you.
*
* @see https://github.com/fastify/fastify-bearer-auth?tab=readme-ov-file#integration-with-fastifyauth
*/
import type { FastifyInstance, FastifyReply, FastifyRequest, RouteShorthandOptions } from 'fastify';
import type { Server } from 'http';
import type { IReqPokeHello } from '../../interfaces/IReqPokeHello';
Expand All @@ -8,6 +16,8 @@ export const option: (fastify: FastifyInstance) => RouteShorthandOptions = () =>
querystring: schema.properties?.Querystring,
body: schema.properties?.Body,
},
// preHandler hook using fastify instance
preHandler: fastify.auth([fastify.allowAnonymous, fastify.verifyBearerAuth]),
});

export const handler = async (
Expand Down
Loading