Skip to content

Commit

Permalink
docs: sync up type documentation with implementation (sveltejs#1430)
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatiusmb authored and andyburke committed May 25, 2021
1 parent 35c1108 commit f1ea703
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
26 changes: 14 additions & 12 deletions documentation/docs/01-routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,29 @@ A file or directory can have multiple dynamic parts, like `[id]-[category].svelt
Endpoints are modules written in `.js` (or `.ts`) files that export functions corresponding to HTTP methods. For example, our hypothetical blog page, `/blog/cool-article`, might request data from `/blog/cool-article.json`, which could be represented by a `src/routes/blog/[slug].json.js` endpoint:

```ts
type Request<Context = any> = {
type Headers = Record<string, string>;

type Request<Locals = Record<string, any>, Body = unknown> = {
method: string;
host: string;
method: 'GET';
headers: Record<string, string>;
headers: Headers;
path: string;
params: Record<string, string | string[]>;
params: Record<string, string>;
query: URLSearchParams;
rawBody: string | Uint8Array;
body: string | Uint8Array | JSONValue;
locals: Record<string, any>; // see below
body: ParameterizedBody<Body>;
locals: Locals; // populated by hooks handle
};

type Response = {
type EndpointOutput = {
status?: number;
headers?: Record<string, string>;
body?: any;
headers?: Headers;
body?: string | Uint8Array | JSONValue;
};

type RequestHandler<Context = any> = {
(request: Request<Context>) => Response | Promise<Response>;
}
type RequestHandler<Locals = Record<string, any>> = (
request: Request<Locals>
) => void | EndpointOutput | Promise<EndpointOutput>;
```

```js
Expand Down
4 changes: 2 additions & 2 deletions documentation/docs/03-loading.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type LoadInput = {
page: {
host: string;
path: string;
params: Record<string, string | string[]>;
params: Record<string, string>;
query: URLSearchParams;
};
fetch: (info: RequestInfo, init?: RequestInit) => Promise<Response>;
Expand All @@ -21,7 +21,7 @@ type LoadInput = {

type LoadOutput = {
status?: number;
error?: Error;
error?: string | Error;
redirect?: string;
props?: Record<string, any>;
context?: Record<string, any>;
Expand Down
18 changes: 10 additions & 8 deletions documentation/docs/04-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ If unimplemented, defaults to `({ request, render }) => render(request)`.
To add custom data to the request, which is passed to endpoints, populate the `request.locals` object, as shown below.

```ts
type Headers = Record<string, string>;

type Request<Locals = Record<string, any>> = {
method: string;
host: string;
Expand All @@ -23,19 +25,19 @@ type Request<Locals = Record<string, any>> = {
params: Record<string, string>;
query: URLSearchParams;
rawBody: string | Uint8Array;
body: string | Uint8Array | ReadOnlyFormData | JSONValue;
locals: Locals;
body: ParameterizedBody<Body>;
locals: Locals; // populated by hooks handle
};

type Response = {
status?: number;
headers?: Headers;
body?: any;
status: number;
headers: Headers;
body?: string | Uint8Array;
};

type Handle<Locals = Record<string, any>> = ({
request: Request<Locals>,
render: (request: Request<Locals>) => Promise<Response>
type Handle<Locals = Record<string, any>> = (input: {
request: Request<Locals>;
render: (request: Request<Locals>) => Response | Promise<Response>;
}) => Response | Promise<Response>;
```

Expand Down

0 comments on commit f1ea703

Please sign in to comment.