Skip to content

Commit

Permalink
chore: lint and format
Browse files Browse the repository at this point in the history
  • Loading branch information
Barbapapazes committed Sep 12, 2023
1 parent e0ad829 commit e034a51
Show file tree
Hide file tree
Showing 12 changed files with 328 additions and 174 deletions.
39 changes: 25 additions & 14 deletions examples/body.ts
Original file line number Diff line number Diff line change
@@ -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);
42 changes: 27 additions & 15 deletions examples/cookies.ts
Original file line number Diff line number Diff line change
@@ -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);
54 changes: 39 additions & 15 deletions examples/errors.ts
Original file line number Diff line number Diff line change
@@ -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);
70 changes: 44 additions & 26 deletions examples/first-server.ts
Original file line number Diff line number Diff line change
@@ -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 '<h1>hello world</h1>'
}))
.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 "<h1>hello world</h1>";
}),
)
.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);
33 changes: 21 additions & 12 deletions examples/headers.ts
Original file line number Diff line number Diff line change
@@ -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);
33 changes: 20 additions & 13 deletions examples/hooks.ts
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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);
Loading

0 comments on commit e034a51

Please sign in to comment.