Skip to content

spacecloud-io/space-sdk-ts

Repository files navigation

Space SDK (Typescript)

Typescript API for Space Cloud

Worker API

Sample usage

import { Server } from "@spacecloud-io/worker";
import { z } from "zod";

// We first create the server object
const server = Server.create({ 
  name: "myServer",
  baseUrl: "/v2",     // Optional. Defaults to `/v1`.
  port: 8080          // Optional. Defaults to `3000`.
});

// Create a router object. All operations are registered on this router.
const router = server.router();

// Register a query object.
router.query("operate")                       // `opId` is the name of the operation
  .method("GET")                              // Defaults to `GET` for queries and `POST` for mutations
  .url("/v1/operate")                         // Defaults to `${baseUrl}/${opId}`                                                 
  .input(z.object({ name: z.string() }))
  .output(z.object({ greeting: z.string() }))
  .fn(async (_ctx, req) => {
    return { greeting: `Hi ${req.name}` };
  });

// Start the express http server.
server.start();

The worker generates the some additional routes as shown below:

# Routes created to expose the OpenAPI specification generated
[GET] `${baseUrl}/openapi.json`
[GET] `${baseUrl}/openapi.yaml` 

Saving OpenAPI specification to disk

Simply add the flag --save-openapi flag to save the autogenerated Open API specification in yaml format. Use the -f, --file <path> flag to control the file path. The specification will be persisted at ${file}/openapi.yaml.

node index.js --save-openapi -f ./config