Skip to content

Commit

Permalink
add typescript documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
terrablue committed Jan 27, 2024
1 parent 95d60dd commit b40fe58
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 12 deletions.
3 changes: 1 addition & 2 deletions docs/guide/responses.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ response.

### JSON

Objects (including arrays) are served with the content type
`application/json`.
Objects (including arrays) are served with the content type `application/json`.

```js caption=routes/json.js
export default {
Expand Down
3 changes: 2 additions & 1 deletion docs/guide/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ be strings.

!!!
The `base` property is relevant when you use other languages aside from
JavaScript (like Go)
JavaScript (like Go).
!!!

You can also create more elaborate types, like `uuid`.

Expand Down
56 changes: 56 additions & 0 deletions docs/modules/typescript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# TypeScript

This binding introduces support for routes written in TypeScript.

## Install

`npm install @primate/binding @swc/core@1.3`

## Configure

Import and initialize the module in your configuration.

```js caption=primate.config.js
import { typescript } from "@primate/binding";

export default {
modules: [
typescript(),
],
};
```

## Use

Using TypeScript is identical to using JavaScript, with the exception that to
get proper editor completions for your route function parameters and return
code, your route needs to use `satisfies Route` with the Primate `Route` export.

```ts caption=routes/plain-text.ts
import { Route } from "primate";

export default {
get() {
return "Donald";
},
} satisfies Route;
```

!!!
Using `satisfies Route` is optional and will only result in your editor showing
your proper completions.
!!!

Refer to the [routes] and [responses] documentation for the different aspects
of handling requests and returning responses.

## Configuration options

### extension

Default `".ts"`

The file extension associated with TypeScript routes.

[routes]: /guide/routes
[responses]: /guide/responses
2 changes: 1 addition & 1 deletion packages/binding/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@ruby/wasm-wasi": {
"optional": true
},
"swc/core" :{
"@swc/core": {
"optional": true
},
"pyodide": {
Expand Down
10 changes: 5 additions & 5 deletions packages/primate/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ declare module "primate" {

type Dispatcher = {
get(property: string): string,
}
};

type RequestFacade = {
body: {}
Expand All @@ -25,7 +25,7 @@ declare module "primate" {
cookies: Dispatcher,
headers: Dispatcher,
original: Request,
}
};

type ResponseFn = (app: App, ...rest: any) => Response;
type ResponseFacade =
Expand All @@ -37,16 +37,16 @@ declare module "primate" {
| Response
| ResponseFn;

type RouteFunction = (request: RequestFacade) => ResponseFacade;
type RouteFunction = (request?: RequestFacade) => ResponseFacade;

type Streamable = ReadableStream;
type Streamable = ReadableStream | Blob;

export type Route = {
get?: RouteFunction,
post?: RouteFunction,
put?: RouteFunction,
delete?: RouteFunction,
}
};

export function text(body: string, options?: MinOptions): ResponseFn;

Expand Down
9 changes: 6 additions & 3 deletions packages/website/primate.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import hljs from "highlight.js/lib/core";
import xml from "highlight.js/lib/languages/xml";

import js from "highlight.js/lib/languages/javascript";
import ts from "highlight.js/lib/languages/typescript";
import json from "highlight.js/lib/languages/json";
import bash from "highlight.js/lib/languages/bash";
import http from "highlight.js/lib/languages/http";
Expand All @@ -17,6 +18,7 @@ import python from "highlight.js/lib/languages/python";
import priss from "./module.js";

hljs.registerLanguage("js", js);
hljs.registerLanguage("ts", ts);
hljs.registerLanguage("json", json);
hljs.registerLanguage("xml", xml);
hljs.registerLanguage("bash", bash);
Expand Down Expand Up @@ -46,7 +48,7 @@ export default {
hooks: {
preprocess(html) {
return html.replaceAll(/%%%(.*?)\n(.*?)%%%/gus, (_, p1, p2) => {
const t =
const t =
p2
.split("\n```")
.filter(p => p !== "" && !p.startsWith("\n"))
Expand All @@ -57,8 +59,8 @@ export default {
</div>`).join("");
return `<div class="tabbed"><span class="captions">${
p1.split(",").map((caption, i) => `<span${i === 0 ? " class='active'" : ""}>${caption}</span>`).join("")
}</span><span class="tabs">${t}</span></div>`;
p1.split(",").map((caption, i) => `<span${i === 0 ? " class='active'" : ""}>${caption}</span>`).join("")
}</span><span class="tabs">${t}</span></div>`;
});
},
postprocess(html) {
Expand Down Expand Up @@ -159,6 +161,7 @@ export default {
"Drivers",
{ heading: "Bindings" },
"Binding",
"TypeScript",
"Go",
"Python",
{ heading: "Others" },
Expand Down

0 comments on commit b40fe58

Please sign in to comment.