Skip to content

spacecloud-io/space-sdk-ts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

1e4d7f5 · Sep 24, 2023

History

4 Commits
Aug 6, 2023
Sep 24, 2023
Sep 24, 2023
Sep 24, 2023
May 31, 2023
Sep 24, 2023
Sep 24, 2023
Sep 24, 2023
Jun 11, 2023

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