From e034a51a24527bf9bf609a305c1f9133d9a3a084 Mon Sep 17 00:00:00 2001 From: barbapapazes Date: Tue, 12 Sep 2023 22:35:58 +0200 Subject: [PATCH] chore: lint and format --- examples/body.ts | 39 ++++++++++++++-------- examples/cookies.ts | 42 +++++++++++++++--------- examples/errors.ts | 54 ++++++++++++++++++++++--------- examples/first-server.ts | 70 +++++++++++++++++++++++++--------------- examples/headers.ts | 33 ++++++++++++------- examples/hooks.ts | 33 +++++++++++-------- examples/http-methods.ts | 65 +++++++++++++++++++++++++------------ examples/query-params.ts | 26 +++++++++------ examples/redirects.ts | 32 ++++++++++++------ examples/status.ts | 67 ++++++++++++++++++++++++-------------- examples/url-params.ts | 35 +++++++++++++------- package.json | 6 ++-- 12 files changed, 328 insertions(+), 174 deletions(-) diff --git a/examples/body.ts b/examples/body.ts index 02cbf604..94f4451b 100644 --- a/examples/body.ts +++ b/examples/body.ts @@ -1,23 +1,34 @@ import { createServer } from "node:http"; -import { createApp, createRouter, defineEventHandler, readBody, toNodeListener } from "h3" +import { + createApp, + createRouter, + defineEventHandler, + readBody, + toNodeListener, +} from "h3"; const app = createApp(); const router = createRouter() - .get('/', defineEventHandler(() => { - return 'use another HTTP method to enable body parsing' - })) - .post('/', defineEventHandler(async (event) => { - const body = await readBody(event) + .get( + "/", + defineEventHandler(() => { + return "use another HTTP method to enable body parsing"; + }), + ) + .post( + "/", + defineEventHandler(async (event) => { + const body = await readBody(event); - // Use can also use `readFormData` to get a FormData object, `readMultiPartFormData` to get an array of MultiPartData or `readRawBody` to get a Buffer. + // Use can also use `readFormData` to get a FormData object, `readMultiPartFormData` to get an array of MultiPartData or `readRawBody` to get a Buffer. - return { - body, - } - })) + return { + body, + }; + }), + ); +app.use(router); -app.use(router) - -createServer(toNodeListener(app)).listen(3000) +createServer(toNodeListener(app)).listen(3000); diff --git a/examples/cookies.ts b/examples/cookies.ts index 61c65409..2ef11c11 100644 --- a/examples/cookies.ts +++ b/examples/cookies.ts @@ -1,24 +1,36 @@ import { createServer } from "node:http"; -import { createApp, createRouter, defineEventHandler, getCookie, setCookie, toNodeListener } from "h3" +import { + createApp, + createRouter, + defineEventHandler, + getCookie, + setCookie, + toNodeListener, +} from "h3"; const app = createApp(); const router = createRouter() - .get('/', defineEventHandler((event) => { - const sessionId = getCookie(event, 'sessionId') + .get( + "/", + defineEventHandler((event) => { + const sessionId = getCookie(event, "sessionId"); - return { - sessionId, - } - })) - .post('/login', defineEventHandler((event) => { - setCookie(event, "sessionId", "bar", { httpOnly: true }) // By default, path is set to `/`. You can use any of the options supported by the Set-Cookie header. - // @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie + return { + sessionId, + }; + }), + ) + .post( + "/login", + defineEventHandler((event) => { + setCookie(event, "sessionId", "bar", { httpOnly: true }); // By default, path is set to `/`. You can use any of the options supported by the Set-Cookie header. + // @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie - return 'logged in' - })) + return "logged in"; + }), + ); +app.use(router); -app.use(router) - -createServer(toNodeListener(app)).listen(3000) +createServer(toNodeListener(app)).listen(3000); diff --git a/examples/errors.ts b/examples/errors.ts index a231df42..24d6024c 100644 --- a/examples/errors.ts +++ b/examples/errors.ts @@ -1,22 +1,46 @@ import { createServer } from "node:http"; -import { createApp, createError, createRouter, defineEventHandler, toNodeListener } from "h3" +import { + createApp, + createError, + createRouter, + defineEventHandler, + toNodeListener, +} from "h3"; const app = createApp(); const router = createRouter() - .get('/', defineEventHandler(() => { - // Do not forget to return the error - return createError('A simple error') // Create a 500 Internal Server Error by default - })) - .get('/complexe-error', defineEventHandler(() => { - // You can fully customize errors by adding data, cause and if it's a fatal error or not - return createError({ status: 400, message: 'Bad request', statusMessage: 'Bad request message' }) - })) - .get('/fatal-error', defineEventHandler(() => { - // Fatal errors will stop the execution of the current request and will be logged - return createError({ status: 500, message: 'Fatal error', fatal: true, data: { foo: 'bar' } }) - })) + .get( + "/", + defineEventHandler(() => { + // Do not forget to return the error + return createError("A simple error"); // Create a 500 Internal Server Error by default + }), + ) + .get( + "/complexe-error", + defineEventHandler(() => { + // You can fully customize errors by adding data, cause and if it's a fatal error or not + return createError({ + status: 400, + message: "Bad request", + statusMessage: "Bad request message", + }); + }), + ) + .get( + "/fatal-error", + defineEventHandler(() => { + // Fatal errors will stop the execution of the current request and will be logged + return createError({ + status: 500, + message: "Fatal error", + fatal: true, + data: { foo: "bar" }, + }); + }), + ); -app.use(router) +app.use(router); -createServer(toNodeListener(app)).listen(3000) +createServer(toNodeListener(app)).listen(3000); diff --git a/examples/first-server.ts b/examples/first-server.ts index 965f3b3a..5fa04dd7 100644 --- a/examples/first-server.ts +++ b/examples/first-server.ts @@ -1,35 +1,53 @@ import { createServer } from "node:http"; -import { createApp, defineEventHandler, toNodeListener } from "h3" +import { createApp, defineEventHandler, toNodeListener } from "h3"; const app = createApp(); app // `/` is the root path and will response to every request. - .use('/first-request', defineEventHandler(() => { - return "hello world" - })) - .use('/hello', defineEventHandler(() => { - return "world" - })) - .use('/json', defineEventHandler(() => { - // Automatically set the `Content-Type` header to `application/json`. - return { - hello: 'world' - } - })) - .use('/html', defineEventHandler(() => { - // By default, the `Content-Type` header is set to `text/html`. - return '

hello world

' - })) - .use('/buffer', defineEventHandler(() => { - // No `Content-Type` header is set by default. You can set it manually using `setHeader`. - return Buffer.from('hello world') - })) - .use('/blob', defineEventHandler(() => { - // No `Content-Type` header is set by default. You can set it manually using `setHeader`. - return new Blob(['hello world']) - })) + .use( + "/first-request", + defineEventHandler(() => { + return "hello world"; + }), + ) + .use( + "/hello", + defineEventHandler(() => { + return "world"; + }), + ) + .use( + "/json", + defineEventHandler(() => { + // Automatically set the `Content-Type` header to `application/json`. + return { + hello: "world", + }; + }), + ) + .use( + "/html", + defineEventHandler(() => { + // By default, the `Content-Type` header is set to `text/html`. + return "

hello world

"; + }), + ) + .use( + "/buffer", + defineEventHandler(() => { + // No `Content-Type` header is set by default. You can set it manually using `setHeader`. + return Buffer.from("hello world"); + }), + ) + .use( + "/blob", + defineEventHandler(() => { + // No `Content-Type` header is set by default. You can set it manually using `setHeader`. + return new Blob(["hello world"]); + }), + ); // The `use` method respond to every HTTP method by default. -createServer(toNodeListener(app)).listen(3000) +createServer(toNodeListener(app)).listen(3000); diff --git a/examples/headers.ts b/examples/headers.ts index ffa440fe..6e9a07d5 100644 --- a/examples/headers.ts +++ b/examples/headers.ts @@ -1,30 +1,39 @@ import { createServer } from "node:http"; -import { createApp, createRouter, defineEventHandler, getRequestHeader, getResponseHeaders, setResponseHeader, toNodeListener } from "h3" +import { + createApp, + createRouter, + defineEventHandler, + getRequestHeader, + getResponseHeaders, + setResponseHeader, + toNodeListener, +} from "h3"; const app = createApp(); -const router = createRouter() - .get('/user-agent', defineEventHandler((event) => { - const agent = getRequestHeader(event, 'user-agent') +const router = createRouter().get( + "/user-agent", + defineEventHandler((event) => { + const agent = getRequestHeader(event, "user-agent"); // You can also use `getRequestHeaders` to get all headers at once. // const headers = getRequestHeaders(event) - setResponseHeader(event, 'content-type', 'text/plain') - setResponseHeader(event, 'x-server', 'nitro') + setResponseHeader(event, "content-type", "text/plain"); + setResponseHeader(event, "x-server", "nitro"); // You can also use `setResponseHeaders` to set multiple headers at once. // setResponseHeaders(event, { 'x-server': 'nitro', 'content-type': 'text/plain' }) - const responseHeaders = getResponseHeaders(event) + const responseHeaders = getResponseHeaders(event); // You can also use `getResponseHeader` to get a single header. // const contentType = getResponseHeader(event, 'content-type') return { agent, responseHeaders, - } - })) + }; + }), +); +app.use(router); -app.use(router) - -createServer(toNodeListener(app)).listen(3000) +createServer(toNodeListener(app)).listen(3000); diff --git a/examples/hooks.ts b/examples/hooks.ts index fbe8541a..3198a0a7 100644 --- a/examples/hooks.ts +++ b/examples/hooks.ts @@ -1,5 +1,12 @@ import { createServer } from "node:http"; -import { createApp, createRouter, defineEventHandler, defineRequestMiddleware, defineResponseMiddleware, toNodeListener } from "h3"; +import { + createApp, + createRouter, + defineEventHandler, + defineRequestMiddleware, + defineResponseMiddleware, + toNodeListener, +} from "h3"; // const app = createApp() @@ -24,23 +31,23 @@ import { createApp, createRouter, defineEventHandler, defineRequestMiddleware, d // createServer(toNodeListener(app)).listen(3000) +const app = createApp(); - -const app = createApp() - -const router = createRouter() - .get('/', defineEventHandler({ +const router = createRouter().get( + "/", + defineEventHandler({ onRequest: defineRequestMiddleware(() => { - console.log('onRequest') + console.log("onRequest"); }), handler: defineEventHandler(() => { - return "GET: hello world" + return "GET: hello world"; }), onBeforeResponse: defineResponseMiddleware(() => { - console.log('onResponse') - }) - })) + console.log("onResponse"); + }), + }), +); -app.use(router) +app.use(router); -createServer(toNodeListener(app)).listen(3000) +createServer(toNodeListener(app)).listen(3000); diff --git a/examples/http-methods.ts b/examples/http-methods.ts index f0b1768f..1810c69a 100644 --- a/examples/http-methods.ts +++ b/examples/http-methods.ts @@ -1,28 +1,51 @@ import { createServer } from "node:http"; -import { createApp, createRouter, defineEventHandler, toNodeListener } from "h3" +import { + createApp, + createRouter, + defineEventHandler, + toNodeListener, +} from "h3"; const app = createApp(); const router = createRouter() - .get('/', defineEventHandler(() => { - return "GET: hello world" - })) - .post('/', defineEventHandler(() => { - return "POST: hello world" - })) - .put('/', defineEventHandler(() => { - return "PUT: hello world" - })) - .delete('/', defineEventHandler(() => { - return "DELETE: hello world" - })) - .patch('/', defineEventHandler(() => { - return "PATCH: hello world" - })) - .head('/', defineEventHandler(() => { - return "HEAD: hello world" - })) + .get( + "/", + defineEventHandler(() => { + return "GET: hello world"; + }), + ) + .post( + "/", + defineEventHandler(() => { + return "POST: hello world"; + }), + ) + .put( + "/", + defineEventHandler(() => { + return "PUT: hello world"; + }), + ) + .delete( + "/", + defineEventHandler(() => { + return "DELETE: hello world"; + }), + ) + .patch( + "/", + defineEventHandler(() => { + return "PATCH: hello world"; + }), + ) + .head( + "/", + defineEventHandler(() => { + return "HEAD: hello world"; + }), + ); -app.use(router) +app.use(router); -createServer(toNodeListener(app)).listen(3000) +createServer(toNodeListener(app)).listen(3000); diff --git a/examples/query-params.ts b/examples/query-params.ts index 44407a91..a7020b2f 100644 --- a/examples/query-params.ts +++ b/examples/query-params.ts @@ -1,19 +1,27 @@ import { createServer } from "node:http"; -import { createApp, createRouter, defineEventHandler, getQuery, toNodeListener } from "h3" +import { + createApp, + createRouter, + defineEventHandler, + getQuery, + toNodeListener, +} from "h3"; const app = createApp(); -const router = createRouter() - .get('/', defineEventHandler((event) => { - const query = getQuery(event) +const router = createRouter().get( + "/", + defineEventHandler((event) => { + const query = getQuery(event); if (!query.name) { - return 'Hello world' + return "Hello world"; } - return `Hello ${query.name}` - })) + return `Hello ${query.name}`; + }), +); -app.use(router) +app.use(router); -createServer(toNodeListener(app)).listen(3000) +createServer(toNodeListener(app)).listen(3000); diff --git a/examples/redirects.ts b/examples/redirects.ts index 25c5c4e6..115e7359 100644 --- a/examples/redirects.ts +++ b/examples/redirects.ts @@ -1,17 +1,29 @@ import { createServer } from "node:http"; -import { createApp, createRouter, defineEventHandler, sendRedirect, toNodeListener } from "h3" +import { + createApp, + createRouter, + defineEventHandler, + sendRedirect, + toNodeListener, +} from "h3"; const app = createApp(); const router = createRouter() - .get('/unjs', defineEventHandler((event) => { - return sendRedirect(event, 'https://unjs.io/packages/h3') // 302 Found by default - })) - .get('/permanent', defineEventHandler((event) => { - // You can use any 3xx status code you want - return sendRedirect(event, 'https://unjs.io/packages/h3', 301) - })) + .get( + "/unjs", + defineEventHandler((event) => { + return sendRedirect(event, "https://unjs.io/packages/h3"); // 302 Found by default + }), + ) + .get( + "/permanent", + defineEventHandler((event) => { + // You can use any 3xx status code you want + return sendRedirect(event, "https://unjs.io/packages/h3", 301); + }), + ); -app.use(router) +app.use(router); -createServer(toNodeListener(app)).listen(3000) +createServer(toNodeListener(app)).listen(3000); diff --git a/examples/status.ts b/examples/status.ts index a8bb8b57..11b967bf 100644 --- a/examples/status.ts +++ b/examples/status.ts @@ -1,30 +1,47 @@ import { createServer } from "node:http"; -import { createApp, createRouter, defineEventHandler, getResponseStatus, getResponseStatusText, sendNoContent, setResponseStatus, toNodeListener } from "h3" +import { + createApp, + createRouter, + defineEventHandler, + getResponseStatus, + getResponseStatusText, + sendNoContent, + setResponseStatus, + toNodeListener, +} from "h3"; const app = createApp(); const router = createRouter() - .get('/not-found', defineEventHandler((event) => { - setResponseStatus(event, 404) - - return 'Not found' // You need to explicitly return something to avoid a 404 'Cannot find any path matching "/not-found"' response. - })) - .get('/bad-request', defineEventHandler((event) => { - setResponseStatus(event, 400, 'Bad request message') // You can customize the status message. - - const status = getResponseStatus(event) // You can get the status message. - const text = getResponseStatusText(event) // You can get the status message. - - return { - status, - text, - } - })) - .get('/no-content', defineEventHandler((event) => { - sendNoContent(event) // Do not need to explicitly return because `sendNoContent` will cut the connection. - })) - - -app.use(router) - -createServer(toNodeListener(app)).listen(3000) + .get( + "/not-found", + defineEventHandler((event) => { + setResponseStatus(event, 404); + + return "Not found"; // You need to explicitly return something to avoid a 404 'Cannot find any path matching "/not-found"' response. + }), + ) + .get( + "/bad-request", + defineEventHandler((event) => { + setResponseStatus(event, 400, "Bad request message"); // You can customize the status message. + + const status = getResponseStatus(event); // You can get the status message. + const text = getResponseStatusText(event); // You can get the status message. + + return { + status, + text, + }; + }), + ) + .get( + "/no-content", + defineEventHandler((event) => { + sendNoContent(event); // Do not need to explicitly return because `sendNoContent` will cut the connection. + }), + ); + +app.use(router); + +createServer(toNodeListener(app)).listen(3000); diff --git a/examples/url-params.ts b/examples/url-params.ts index 9db13253..aca783b5 100644 --- a/examples/url-params.ts +++ b/examples/url-params.ts @@ -1,19 +1,32 @@ import { createServer } from "node:http"; -import { createApp, createRouter, defineEventHandler, toNodeListener, getRouterParam, getRouterParams } from "h3" +import { + createApp, + createRouter, + defineEventHandler, + toNodeListener, + getRouterParam, + getRouterParams, +} from "h3"; const app = createApp(); const router = createRouter() - .get('/:name', defineEventHandler((event) => { - const name = getRouterParam(event, 'name') - return `Hello ${name}` - })) - .get('/:name/:age', defineEventHandler((event) => { - const params = getRouterParams(event) + .get( + "/:name", + defineEventHandler((event) => { + const name = getRouterParam(event, "name"); + return `Hello ${name}`; + }), + ) + .get( + "/:name/:age", + defineEventHandler((event) => { + const params = getRouterParams(event); - return `Hello ${params.name}, you are ${params.age} years old` - })) + return `Hello ${params.name}, you are ${params.age} years old`; + }), + ); -app.use(router) +app.use(router); -createServer(toNodeListener(app)).listen(3000) +createServer(toNodeListener(app)).listen(3000); diff --git a/package.json b/package.json index 7f253022..4ef83286 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,8 @@ "scripts": { "build": "unbuild", "dev": "vitest", - "lint": "eslint --cache --ext .ts,.js,.mjs,.cjs . && prettier -c src test playground", - "lint:fix": "eslint --cache --ext .ts,.js,.mjs,.cjs . --fix && prettier -c src test playground -w", + "lint": "eslint --cache --ext .ts,.js,.mjs,.cjs . && prettier -c src test playground examples", + "lint:fix": "eslint --cache --ext .ts,.js,.mjs,.cjs . --fix && prettier -c src test playground examples -w", "play": "listhen -w ./playground/app.ts", "profile": "0x -o -D .profile -P 'autocannon -c 100 -p 10 -d 40 http://localhost:$PORT' ./playground/server.cjs", "release": "pnpm test && pnpm build && changelogen --release && pnpm publish && git push --follow-tags", @@ -67,4 +67,4 @@ "zod": "^3.22.2" }, "packageManager": "pnpm@8.7.1" -} \ No newline at end of file +}