Skip to content

Commit fb5579e

Browse files
vknabelmajst01
andauthored
docs(examples): typescript / javascript (#102)
Co-authored-by: Stefan Majer <stefan.majer@f-i-ts.de>
1 parent 79ed5b9 commit fb5579e

4 files changed

Lines changed: 113 additions & 0 deletions

File tree

examples/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ sh curl/ip-list.sh
2121

2222
# Python:
2323
python python/ip-list.py
24+
25+
# TypeScript / JavaScript:
26+
cd js && bun i && bun run ip-list.ts
2427
```

examples/js/bun.lock

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/js/ip-list.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import * as apiv2 from "@metal-stack/api/js/metalstack/api/v2/ip_pb";
2+
import { createClient } from "@connectrpc/connect";
3+
import { createConnectTransport } from "@connectrpc/connect-web";
4+
5+
import { Code, ConnectError, Interceptor } from "@connectrpc/connect";
6+
7+
class AuthInterceptor {
8+
private authToken: string;
9+
10+
constructor(authToken: string) {
11+
this.authToken = authToken;
12+
}
13+
14+
interceptor: Interceptor = (next) => async (req) => {
15+
if (!this.authToken) {
16+
throw new ConnectError("Missing auth token", Code.Unauthenticated);
17+
}
18+
19+
req.header.append("Authorization", `Bearer ${this.authToken}`);
20+
21+
try {
22+
const res = await next(req);
23+
return res;
24+
} catch (e) {
25+
if (e instanceof ConnectError && e.code === Code.Unauthenticated) {
26+
// e.g. message: "token has expired"
27+
console.error("unauthenticated", e);
28+
}
29+
throw e;
30+
}
31+
};
32+
}
33+
34+
async function main() {
35+
const token = process.env["API_TOKEN"];
36+
const project = process.env["PROJECT_ID"];
37+
const baseUrl = process.env["METAL_APISERVER_URL"];
38+
39+
const auth = new AuthInterceptor(token!);
40+
const client = createClient(
41+
apiv2.IPService,
42+
createConnectTransport({
43+
baseUrl: baseUrl!,
44+
interceptors: [auth.interceptor],
45+
}),
46+
);
47+
48+
const listResp = await client.list({ project });
49+
50+
for (const ip of listResp.ips) {
51+
console.log("ip", ip);
52+
}
53+
}
54+
55+
main();

examples/js/package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "metal-stack-api-example",
3+
"version": "1.0.0",
4+
"private": true,
5+
"description": "@metal-stack/api example",
6+
"main": "ip-list.ts",
7+
"scripts": {
8+
"build": "tsc --project tsconfig.json",
9+
"ip-list": "npm run build && node ./ip-list.js"
10+
},
11+
"author": "metal-stack",
12+
"license": "MIT",
13+
"dependencies": {
14+
"@connectrpc/connect": "^2.1.1",
15+
"@connectrpc/connect-web": "^2.1.1",
16+
"@metal-stack/api": "^0.0.47"
17+
},
18+
"devDependencies": {
19+
"@types/node": "^25.2.3",
20+
"typescript": "^5.9.3"
21+
}
22+
}

0 commit comments

Comments
 (0)