Skip to content

Commit 41669b0

Browse files
committed
Add sdk-test-procedure-ts
1 parent 153408c commit 41669b0

File tree

6 files changed

+231
-0
lines changed

6 files changed

+231
-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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// ─────────────────────────────────────────────────────────────────────────────
2+
// IMPORTS
3+
// ─────────────────────────────────────────────────────────────────────────────
4+
import { errors, schema, t, table } from 'spacetimedb/server';
5+
6+
const ReturnStruct = t.object('ReturnStruct', {
7+
a: t.u32(),
8+
b: t.string(),
9+
});
10+
11+
const ReturnEnum = t.enum('ReturnEnum', {
12+
A: t.u32(),
13+
B: t.string(),
14+
});
15+
16+
const spacetimedb = schema();
17+
18+
spacetimedb.procedure(
19+
'return_primitive',
20+
{ lhs: t.u32(), rhs: t.u32() },
21+
t.u32(),
22+
(_ctx, { lhs, rhs }) => lhs + rhs
23+
);
24+
25+
spacetimedb.procedure(
26+
'return_struct',
27+
{ a: t.u32(), b: t.string() },
28+
ReturnStruct,
29+
(_ctx, { a, b }) => ({ a, b })
30+
);
31+
32+
spacetimedb.procedure(
33+
'return_enum_a',
34+
{ a: t.u32() },
35+
ReturnEnum,
36+
(_ctx, { a }) => ReturnEnum.A(a)
37+
);
38+
39+
spacetimedb.procedure(
40+
'return_enum_b',
41+
{ b: t.string() },
42+
ReturnEnum,
43+
(_ctx, { b }) => ReturnEnum.B(b)
44+
);
45+
46+
spacetimedb.procedure('will_panic', t.unit(), _ctx => {
47+
throw new Error('This procedure is expected to panic');
48+
});
49+
50+
spacetimedb.procedure('read_my_schema', t.string(), ctx => {
51+
const module_identity = ctx.identity;
52+
const response = ctx.http.fetch(
53+
`http://localhost:3000/v1/database/${module_identity}/schema?version=9`
54+
);
55+
return response.text();
56+
});
57+
58+
spacetimedb.procedure('invalid_request', t.string(), ctx => {
59+
try {
60+
const response = ctx.http.fetch('http://foo.invalid/');
61+
throw new Error(
62+
`Got result from requesting \`http://foo.invalid\`... huh?\n${response.text()}`
63+
);
64+
} catch (e) {
65+
if (e instanceof errors.HttpError) {
66+
return e.message;
67+
}
68+
throw e;
69+
}
70+
});
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)