Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ sh curl/ip-list.sh

# Python:
python python/ip-list.py

# TypeScript / JavaScript:
cd js && bun i && bun run ip-list.ts
```
33 changes: 33 additions & 0 deletions examples/js/bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions examples/js/ip-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as apiv2 from "@metal-stack/api/js/metalstack/api/v2/ip_pb";
import { createClient } from "@connectrpc/connect";
import { createConnectTransport } from "@connectrpc/connect-web";

import { Code, ConnectError, Interceptor } from "@connectrpc/connect";

class AuthInterceptor {
private authToken: string;

constructor(authToken: string) {
this.authToken = authToken;
}

interceptor: Interceptor = (next) => async (req) => {
if (!this.authToken) {
throw new ConnectError("Missing auth token", Code.Unauthenticated);
}

req.header.append("Authorization", `Bearer ${this.authToken}`);

try {
const res = await next(req);
return res;
} catch (e) {
if (e instanceof ConnectError && e.code === Code.Unauthenticated) {
// e.g. message: "token has expired"
console.error("unauthenticated", e);
}
throw e;
}
};
}

async function main() {
const token = process.env["API_TOKEN"];
const project = process.env["PROJECT_ID"];
const baseUrl = process.env["METAL_APISERVER_URL"];

const auth = new AuthInterceptor(token!);
const client = createClient(
apiv2.IPService,
createConnectTransport({
baseUrl: baseUrl!,
interceptors: [auth.interceptor],
}),
);

const listResp = await client.list({ project });

for (const ip of listResp.ips) {
console.log("ip", ip);
}
}

main();
22 changes: 22 additions & 0 deletions examples/js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "metal-stack-api-example",
"version": "1.0.0",
"private": true,
"description": "@metal-stack/api example",
"main": "ip-list.ts",
"scripts": {
"build": "tsc --project tsconfig.json",
"ip-list": "npm run build && node ./ip-list.js"
},
Comment on lines +7 to +10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we use bun I don't think we need this. bun supports out of the box execution of .ts files and transpiles them on the fly

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would stick with npm, because these are examples and most end users know npm better than bun. How we build the project is obviously something different.

"author": "metal-stack",
"license": "MIT",
"dependencies": {
"@connectrpc/connect": "^2.1.1",
"@connectrpc/connect-web": "^2.1.1",
"@metal-stack/api": "^0.0.47"
},
"devDependencies": {
"@types/node": "^25.2.3",
"typescript": "^5.9.3"
}
}
Loading