Skip to content

Commit cb3ac50

Browse files
authored
[TS] Http procedure API (#3731)
# Description of Changes Provides a fetch-alike API on `ctx.http`. I guess it could just be `ctx.fetch()` instead of `ctx.http.fetch()`, but I'm not sure if that's a good idea. # Expected complexity level and risk 2 # Testing <!-- Describe any testing you've done, and any testing you'd like your reviewers to do, so that you're confident that all the changes work as expected! --> - [x] Need to verify that this actually works - [ ] <!-- maybe a test you want a reviewer to do, so they can check it off when they're satisfied. -->
1 parent b17e0db commit cb3ac50

34 files changed

+935
-66
lines changed

crates/bindings-typescript/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@
158158
"dependencies": {
159159
"base64-js": "^1.5.1",
160160
"fast-text-encoding": "^1.0.0",
161-
"prettier": "^3.3.3"
161+
"headers-polyfill": "^4.0.3",
162+
"prettier": "^3.3.3",
163+
"statuses": "^2.0.2",
164+
"url-polyfill": "^1.1.14"
162165
},
163166
"peerDependencies": {
164167
"react": "^18.0.0 || ^19.0.0-0 || ^19.0.0",
@@ -177,6 +180,7 @@
177180
"@size-limit/file": "^11.2.0",
178181
"@types/fast-text-encoding": "^1.0.3",
179182
"@types/react": "^19.1.13",
183+
"@types/statuses": "^2.0.6",
180184
"@typescript-eslint/eslint-plugin": "^8.18.2",
181185
"@typescript-eslint/parser": "^8.18.2",
182186
"@vitest/coverage-v8": "^3.2.4",

crates/bindings-typescript/src/lib/autogen/http_header_pair_type.ts

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

crates/bindings-typescript/src/lib/autogen/http_headers_type.ts

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

crates/bindings-typescript/src/lib/autogen/http_method_type.ts

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

crates/bindings-typescript/src/lib/autogen/http_request_type.ts

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

crates/bindings-typescript/src/lib/autogen/http_response_type.ts

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

crates/bindings-typescript/src/lib/autogen/http_version_type.ts

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export { default as HttpHeaderPair } from './autogen/http_header_pair_type';
2+
export { default as HttpHeaders } from './autogen/http_headers_type';
3+
export { default as HttpMethod } from './autogen/http_method_type';
4+
export { default as HttpRequest } from './autogen/http_request_type';
5+
export { default as HttpResponse } from './autogen/http_response_type';
6+
export { default as HttpVersion } from './autogen/http_version_type';

crates/bindings-typescript/src/lib/procedures.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { AlgebraicType, ProductType } from '../lib/algebraic_type';
22
import type { ConnectionId } from '../lib/connection_id';
33
import type { Identity } from '../lib/identity';
44
import type { Timestamp } from '../lib/timestamp';
5+
import type { HttpClient } from '../server/http_internal';
56
import type { ParamsObj } from './reducers';
67
import {
78
MODULE_DEF,
@@ -23,6 +24,7 @@ export interface ProcedureCtx<S extends UntypedSchemaDef> {
2324
readonly identity: Identity;
2425
readonly timestamp: Timestamp;
2526
readonly connectionId: ConnectionId | null;
27+
readonly http: HttpClient;
2628
}
2729

2830
export function procedure<

crates/bindings-typescript/src/server/errors.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
export class SpacetimeHostError extends Error {
99
public readonly code: number;
1010
public readonly message: string;
11-
constructor(code: number) {
11+
constructor(code: number, message?: string) {
1212
super();
1313
const proto = Object.getPrototypeOf(this);
1414
let cls;
@@ -24,7 +24,7 @@ export class SpacetimeHostError extends Error {
2424
}
2525
Object.setPrototypeOf(this, cls.prototype);
2626
this.code = cls.CODE;
27-
this.message = cls.MESSAGE;
27+
this.message = message ?? cls.MESSAGE;
2828
}
2929
get name(): string {
3030
return errnoToClass.get(this.code)?.name ?? 'SpacetimeHostError';
@@ -151,6 +151,8 @@ const errorData = {
151151
20,
152152
'ABI call can only be made while within a read-only transaction',
153153
],
154+
155+
HttpError: [21, 'The HTTP request failed'],
154156
} as const;
155157

156158
function mapEntries<const T extends Record<string, any>, U>(

0 commit comments

Comments
 (0)