Skip to content

Commit 7495076

Browse files
committed
Add sdk-test-procedure-ts
1 parent 910ab30 commit 7495076

File tree

6 files changed

+224
-0
lines changed

6 files changed

+224
-0
lines changed

modules/sdk-test-procedure-ts/package-lock.json

Lines changed: 113 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "sdk-test-procedure-module",
3+
"license": "ISC",
4+
"type": "module",
5+
"scripts": {
6+
"build": "cargo build -p spacetimedb-standalone && cargo run -p spacetimedb-cli -- build",
7+
"generate-ts": "cargo build -p spacetimedb-standalone && cargo run -p spacetimedb-cli -- generate --lang typescript --out-dir ts-codegen",
8+
"publish": "cargo run -p spacetimedb-cli -- publish"
9+
},
10+
"dependencies": {
11+
"spacetimedb": "workspace:^"
12+
}
13+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// ─────────────────────────────────────────────────────────────────────────────
2+
// IMPORTS
3+
// ─────────────────────────────────────────────────────────────────────────────
4+
import { toCamelCase, type Infer } from 'spacetimedb';
5+
import { errors, type RowObj, schema, t, table } from 'spacetimedb/server';
6+
7+
const ReturnStruct = t.object('ReturnStruct', {
8+
a: t.u32(),
9+
b: t.string(),
10+
});
11+
12+
const ReturnEnum = t.enum('ReturnEnum', {
13+
A: t.u32(),
14+
B: t.string(),
15+
});
16+
17+
const spacetimedb = schema();
18+
19+
spacetimedb.procedure(
20+
'return_primitive',
21+
{ lhs: t.u32(), rhs: t.u32() },
22+
t.u32(),
23+
(_ctx, { lhs, rhs }) => lhs + rhs
24+
);
25+
26+
spacetimedb.procedure(
27+
'return_struct',
28+
{ a: t.u32(), b: t.string() },
29+
ReturnStruct,
30+
(_ctx, { a, b }) => ({ a, b })
31+
);
32+
33+
spacetimedb.procedure(
34+
'return_enum_a',
35+
{ a: t.u32() },
36+
ReturnEnum,
37+
(_ctx, { a }) => ReturnEnum.A(a)
38+
);
39+
40+
spacetimedb.procedure(
41+
'return_enum_b',
42+
{ b: t.string() },
43+
ReturnEnum,
44+
(_ctx, { b }) => ReturnEnum.B(b)
45+
);
46+
47+
spacetimedb.procedure('will_panic', t.unit(), ctx => {
48+
throw new Error('This procedure is expected to panic');
49+
});
50+
51+
spacetimedb.procedure('invalid_request', t.string(), ctx => {
52+
try {
53+
const response = ctx.http.fetch('http://foo.invalid/');
54+
throw new Error(
55+
`Got result from requesting \`http://foo.invalid\`... huh?\n${response.text()}`
56+
);
57+
} catch (e) {
58+
if (e instanceof errors.HttpError) {
59+
return e.message;
60+
}
61+
throw e;
62+
}
63+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"module": "ESNext",
5+
6+
"strict": true,
7+
"declaration": true,
8+
"emitDeclarationOnly": false,
9+
"noEmit": true,
10+
"skipLibCheck": true,
11+
"forceConsistentCasingInFileNames": true,
12+
"allowImportingTsExtensions": true,
13+
"noImplicitAny": true,
14+
"moduleResolution": "Bundler",
15+
"isolatedDeclarations": false,
16+
17+
// This library is ESM-only, do not import commonjs modules
18+
"esModuleInterop": false,
19+
"allowSyntheticDefaultImports": false,
20+
"useDefineForClassFields": true,
21+
22+
// Crucial when using esbuild/swc/babel instead of tsc emit:
23+
"verbatimModuleSyntax": true,
24+
"isolatedModules": true
25+
},
26+
"include": ["src/index.ts", "tests/**/*"],
27+
"exclude": ["node_modules", "**/__tests__/*", "dist/**/*"]
28+
}

pnpm-lock.yaml

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

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ packages:
88
- 'modules/module-test-ts'
99
- 'modules/quickstart-chat-ts'
1010
- 'modules/sdk-test-connect-disconnect-ts'
11+
- 'modules/sdk-test-procedure-ts'
1112
- 'modules/sdk-test-ts'
1213
- 'docs'

0 commit comments

Comments
 (0)