Skip to content

Commit

Permalink
docs: update APIs and documentation (denoland#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
iuioiua authored Feb 22, 2024
1 parent f646501 commit 9f0d1aa
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 116 deletions.
6 changes: 2 additions & 4 deletions deploy/api/runtime-fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ The return type of `fetch()` is a promise that resolves to a
The Deno Deploy script below makes a `fetch()` request to the GitHub API for
each incoming request, and then returns that response from the handler function.

```js
import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts";

```ts
async function handler(req: Request): Promise<Response> {
const resp = await fetch("https://api.github.com/users/denoland", {
// The init object here has an headers object containing a
Expand All @@ -64,7 +62,7 @@ async function handler(req: Request): Promise<Response> {
});
}

serve(handler);
Deno.serve(handler);
```

[usvstring]: https://developer.mozilla.org/en-US/docs/Web/API/USVString
43 changes: 12 additions & 31 deletions deploy/api/runtime-fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ This example lists the contents of a directory and returns this list as a JSON
object in the response body.

```js
import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts";
async function handler(_req) {
// List the posts in the `blog` directory located at the root
// of the repository.
Expand All @@ -65,7 +63,7 @@ async function handler(_req) {
});
}
serve(handler);
Deno.serve(handler);
```

## `Deno.readFile`
Expand All @@ -90,8 +88,6 @@ This example reads the contents of a file into memory as a byte array, then
returns it as the response body.

```js
import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts";
async function handler(_req) {
// Let's read the README.md file available at the root
// of the repository to explore the available methods.
Expand All @@ -111,7 +107,7 @@ async function handler(_req) {
return new Response(readme);
}

serve(handler);
Deno.serve(handler);
```

> Note: to use this feature, you must link a GitHub repository to your project.
Expand All @@ -130,8 +126,6 @@ Imagine the following file structure on a GitHub repository:
The contents of `mod.ts`:

```ts
import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts";

async function handleRequest(request: Request): Promise<Response> {
const { pathname } = new URL(request.url);

Expand Down Expand Up @@ -169,7 +163,7 @@ async function handleRequest(request: Request): Promise<Response> {
);
}

serve(handleRequest);
Deno.serve(handleRequest);
```

The path provided to the
Expand All @@ -192,14 +186,12 @@ This example reads a text file into memory and returns the contents as the
response body.

```js
import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts";
async function handler(_req) {
const readme = await Deno.readTextFile("./README.md");
event.respondWith(new Response(readme));
return new Response(readme);
}
serve(handler);
Deno.serve(handler);
```

## `Deno.open`
Expand All @@ -225,21 +217,16 @@ The path can be a relative or absolute. It can also be a `file:` URL.
This example opens a file, and then streams the content as the response body.

```js
import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts";
import { readableStreamFromReader } from "https://deno.land/std@$STD_VERSION/streams/conversion.ts";
async function handler(_req) {
// Open the README.md file available at the root of the repository.
const file = await Deno.open("./README.md");
// Turn the `Deno.File` into a `ReadableStream`. This will automatically close
// the file handle when the response is done sending.
const body = readableStreamFromReader(file);
return new Response(body);
// Use the `readable` property, which is a `ReadableStream`. This will
// automatically close the file handle when the response is done sending.
return new Response(file.readable);
}
serve(handler);
Deno.serve(handler);
```

## `Deno.File`
Expand Down Expand Up @@ -304,8 +291,6 @@ This example gets the size of a file, and returns the result as the response
body.

```js
import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts";
async function handler(_req) {
// Get file info of the README.md at the root of the repository.
const info = await Deno.stat("./README.md");
Expand All @@ -316,7 +301,7 @@ async function handler(_req) {
return new Response(`README.md is ${size} bytes large`);
}
serve(handler);
Deno.serve(handler);
```

## `Deno.lstat`
Expand Down Expand Up @@ -372,15 +357,13 @@ This example calls `Deno.realPath()` to get the absolute path of a file in the
root of the repository. The result is returned as the response body.

```ts
import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts";
async function handler(_req) {
const path = await Deno.realPath("./README.md");
return new Response(`The fully resolved path for ./README.md is ${path}`);
}
serve(handler);
Deno.serve(handler);
```

## `Deno.readLink`
Expand All @@ -402,13 +385,11 @@ This example calls `Deno.readLink()` to get the absolute path of a file in the
root of the repository. The result is returned as the response body.

```ts
import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts";
async function handler(_req) {
const path = await Deno.readLink("./my_symlink");
return new Response(`The target path for ./my_symlink is ${path}`);
}
serve(handler);
Deno.serve(handler);
```
4 changes: 1 addition & 3 deletions deploy/api/runtime-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ All the above properties are read only.
## Example

```ts
import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts";

function handler(_req) {
// Create a post request
const request = new Request("https://post.deno.dev", {
Expand All @@ -94,7 +92,7 @@ function handler(_req) {
return fetch(request);
}

serve(handler);
Deno.serve(handler);
```

[cache]: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
Expand Down
4 changes: 1 addition & 3 deletions deploy/api/runtime-response.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ The return type is a `Response` instance.
## Example

```ts
import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts";

function handler(_req) {
// Create a response with html as its body.
const response = new Response("<html> Hello </html>", {
Expand All @@ -82,7 +80,7 @@ function handler(_req) {
return response;
}

serve(handler);
Deno.serve(handler);
```

[clone]: https://developer.mozilla.org/en-US/docs/Web/API/Response/clone
Expand Down
8 changes: 2 additions & 6 deletions deploy/api/runtime-sockets.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ function Deno.connect(options: ConnectOptions): Promise<Conn>
### Example

```js
import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts";
async function handler(_req) {
// Make a TCP connection to example.com
const connection = await Deno.connect({
Expand All @@ -47,7 +45,7 @@ async function handler(_req) {
});
}
serve(handler);
Deno.serve(handler);
```

## `Deno.connectTls`
Expand All @@ -65,8 +63,6 @@ function Deno.connectTls(options: ConnectTlsOptions): Promise<Conn>
### Example

```js
import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts";
async function handler(_req) {
// Make a TLS connection to example.com
const connection = await Deno.connectTls({
Expand All @@ -93,5 +89,5 @@ async function handler(_req) {
});
}
serve(handler);
Deno.serve(handler);
```
11 changes: 4 additions & 7 deletions deploy/tutorials/tutorial-postgres.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,11 @@ your connection string into the "Value" field. Now, press "Add". Your
environment variables is now set.

Let's return back to the editor: to do this, go to the "Overview" tab via the
left navigation menu, and press "Open Playground". Let's start by the `std/http`
module so we can start serving HTTP requests:
left navigation menu, and press "Open Playground". Let's start by serving HTTP
requests using `Deno.serve()`:

```ts
import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts";

serve(async (req) => {
Deno.serve(async (req) => {
return new Response("Not Found", { status: 404 });
});
```
Expand All @@ -105,7 +103,6 @@ Next, let's import the Postgres module, read the connection string from the
environment variables, and create a connection pool.

```ts
import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts";
import * as postgres from "https://deno.land/x/[email protected]/mod.ts";

// Get the connection string from the environment variable "DATABASE_URL"
Expand Down Expand Up @@ -146,7 +143,7 @@ Now that we have a table, we can add the HTTP handlers for the GET and POST
endpoints.

```ts
serve(async (req) => {
Deno.serve(async (req) => {
// Parse the URL and check that the requested endpoint is /todos. If it is
// not, return a 404 response.
const url = new URL(req.url);
Expand Down
7 changes: 3 additions & 4 deletions runtime/manual/advanced/deploying_deno/kinsta.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ To do so, your `package.json` should look like this:
## Example application

```js
import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts";
import { parse } from "https://deno.land/std@$STD_VERSION/flags/mod.ts";
import { parseArgs } from "https://deno.land/std@$STD_VERSION/cli/parse_args.ts";

const { args } = Deno;
const argPort = parse(args).port ? Number(parse(args).port) : 8000;
const port = parseArgs(args).port ? Number(parseArgs(args).port) : 8000;

serve((_req) => new Response("Hello, world"), { port: argPort });
Deno.serve({ port }, (_req) => new Response("Hello, world"));
```

The application itself is self-explanatory. It's crucial not to hardcode the
Expand Down
24 changes: 8 additions & 16 deletions runtime/manual/basics/connecting_to_databases.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ for Deno, to run a GraphQL API server in Deno.
#### Run a GraphQL API server with gql

```ts, ignore
import { Server } from "https://deno.land/std@$STD_VERSION/http/server.ts";
import { GraphQLHTTP } from "https://deno.land/x/gql/mod.ts";
import { makeExecutableSchema } from "https://deno.land/x/[email protected]/mod.ts";
import { gql } from "https://deno.land/x/[email protected]/mod.ts";
Expand All @@ -238,23 +237,16 @@ const resolvers = {
const schema = makeExecutableSchema({ resolvers, typeDefs });
const s = new Server({
handler: async (req) => {
const { pathname } = new URL(req.url);
Deno.serve({ port: 3000 }, async () => {
const { pathname } = new URL(req.url);
return pathname === "/graphql"
? await GraphQLHTTP<Request>({
schema,
graphiql: true,
})(req)
: new Response("Not Found", { status: 404 });
},
port: 3000,
return pathname === "/graphql"
? await GraphQLHTTP<Request>({
schema,
graphiql: true,
})(req)
: new Response("Not Found", { status: 404 });
});
s.listenAndServe();
console.log(`Started on http://localhost:3000`);
```

### Client
Expand Down
6 changes: 3 additions & 3 deletions runtime/manual/basics/env_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ console.log(password);
// "Geheimnis"
```

## `std/flags`
## `std/cli`

The Deno standard library has a
[`std/flags` module](https://deno.land/std/flags/mod.ts) for parsing command
line arguments.
[`std/cli` module](https://deno.land/std/cli/mod.ts) for parsing command line
arguments.

## Special environment variables

Expand Down
30 changes: 11 additions & 19 deletions runtime/manual/node/how_to_with_npm/redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ information from the user to query our API. The second is Redis. We can grab the
node package for Redis using the `npm:` modifier:

```tsx, ignore
import { Server } from "https://deno.land/std@$STD_VERSION/http/server.ts";
import { createClient } from "npm:redis@^4.5";
```

Expand Down Expand Up @@ -54,25 +53,18 @@ variable we can pass to the Github API as a username. We'll then pass the
response back to the user.

```tsx, ignore
const server = new Server({
handler: async (req) => {
const { pathname } = new URL(req.url);
// strip the leading slash
const username = pathname.substring(1);
const resp = await fetch(`https://api.github.com/users/${username}`);
const user = await resp.json();
return new Response(JSON.stringify(user), {
headers: {
"content-type": "application/json",
},
});
}
},
port: 3000,
Deno.serve({ port: 3000 }, async (req) => {
const { pathname } = new URL(req.url);
// strip the leading slash
const username = pathname.substring(1);
const resp = await fetch(`https://api.github.com/users/${username}`);
const user = await resp.json();
return new Response(JSON.stringify(user), {
headers: {
"content-type": "application/json",
},
});
});
server.listenAndServe();
```

We'll run this with:
Expand Down
2 changes: 1 addition & 1 deletion runtime/manual/tools/benchmarker.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ you want to measure. Everything outside of the section between these two calls
will be excluded from the measurement.

```ts, ignore
import { readAll } from "https://deno.land/std@$STD_VERSION/streams/mod.ts";
import { readAll } from "https://deno.land/std@$STD_VERSION/io/read_all.ts";
Deno.bench("foo", async (b) => {
// Open a file that we will act upon.
Expand Down
Loading

0 comments on commit 9f0d1aa

Please sign in to comment.