Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions crates/bindings-typescript/src/lib/autogen/types.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions crates/bindings-typescript/src/lib/http_types.ts

This file was deleted.

20 changes: 10 additions & 10 deletions crates/bindings-typescript/src/lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ export class ModuleContext {
typespace: { types: [] },
tables: [],
reducers: [],
httpHandlers: [],
httpRoutes: [],
types: [],
rowLevelSecurity: [],
schedules: [],
procedures: [],
views: [],
lifeCycleReducers: [],
httpHandlers: [],
httpRoutes: [],
caseConversionPolicy: { tag: 'SnakeCase' },
explicitNames: {
entries: [],
Expand All @@ -221,6 +221,14 @@ export class ModuleContext {
push(module.tables && { tag: 'Tables', value: module.tables });
push(module.reducers && { tag: 'Reducers', value: module.reducers });
push(module.procedures && { tag: 'Procedures', value: module.procedures });
push(module.views && { tag: 'Views', value: module.views });
push(module.schedules && { tag: 'Schedules', value: module.schedules });
push(
module.lifeCycleReducers && {
tag: 'LifeCycleReducers',
value: module.lifeCycleReducers,
}
);
push(
module.httpHandlers && {
tag: 'HttpHandlers',
Expand All @@ -233,14 +241,6 @@ export class ModuleContext {
value: module.httpRoutes,
}
);
push(module.views && { tag: 'Views', value: module.views });
push(module.schedules && { tag: 'Schedules', value: module.schedules });
push(
module.lifeCycleReducers && {
tag: 'LifeCycleReducers',
value: module.lifeCycleReducers,
}
);
push(
module.rowLevelSecurity && {
tag: 'RowLevelSecurity',
Expand Down
80 changes: 80 additions & 0 deletions crates/bindings-typescript/src/server/http.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { table } from '../lib/table';
import t from '../lib/type_builders';
import {
type HandlerContext,
Request,
SyncResponse,
Router,
schema,
} from './index';

const person = table(
{},
{
id: t.u32().primaryKey(),
name: t.string(),
}
);

const stdb = schema({ person });

const hello = stdb.httpHandler((ctx, req) => {
void ctx.identity;
void ctx.random;
req.text();
req.json();

ctx.withTx(tx => {
tx.db.person.insert({ id: 1, name: 'alice' });
});

return new SyncResponse('hello', {
headers: { 'content-type': 'text/plain' },
status: 200,
});
});

const _typedHello: (ctx: HandlerContext<any>, req: Request) => SyncResponse = (
ctx,
req
) => {
void ctx.timestamp;
return new SyncResponse(req.text());
};

const named = stdb.httpHandler({ name: 'hello' }, (_ctx, _req) => {
return new SyncResponse('named');
});

const routes = stdb.httpRouter(
new Router()
.get('/hello', hello)
.get('/named', named)
.post('/hello-post', hello)
.nest('/api', new Router().any('/v1', hello))
.merge(new Router().get('', hello))
);

void routes;

// @ts-expect-error handlers must return SyncResponse
stdb.httpHandler((_ctx, _req) => 123);

// @ts-expect-error handlers must take HandlerContext as the first argument
stdb.httpHandler((_ctx: number, _req: Request) => new SyncResponse('bad'));

// @ts-expect-error handlers must take a Request as the second argument
stdb.httpHandler((_ctx, _req: number) => new SyncResponse('bad'));

stdb.httpHandler((ctx, req) => {
// @ts-expect-error HTTP handlers do not expose sender directly
void ctx.sender;
// @ts-expect-error HTTP handlers do not expose connectionId directly
void ctx.connectionId;
// @ts-expect-error HTTP handlers do not expose db directly
void ctx.db;
return new SyncResponse(req.text());
});

// @ts-expect-error routers must reference exported http handlers, not raw functions
new Router().get('/raw', (_ctx, _req) => new SyncResponse('bad'));
16 changes: 14 additions & 2 deletions crates/bindings-typescript/src/server/http.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
export { Headers, SyncResponse } from './http_internal';
export type { BodyInit, HeadersInit, ResponseInit } from './http_internal';
export {
type BodyInit,
type HeadersInit,
type RequestInit,
type ResponseInit,
Headers,
Request,
SyncResponse,
Router,
type HandlerContext,
type HandlerFn,
type HttpHandlerExport,
type HttpHandlerOpts,
} from './http_handlers';
Loading
Loading