@ts-pf/astro 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -0
- package/dist/client.d.ts +14 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +36 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/server.d.ts +19 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +44 -0
- package/dist/server.js.map +1 -0
- package/dist/with-base.d.ts +3 -0
- package/dist/with-base.d.ts.map +1 -0
- package/dist/with-base.js +14 -0
- package/dist/with-base.js.map +1 -0
- package/package.json +67 -0
- package/skills/ts-pf-astro/SKILL.md +79 -0
package/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# @ts-pf/astro
|
|
2
|
+
|
|
3
|
+
Opt-in Astro endpoint and in-process page caller for ts-pf. A thin shell around `FetchHandler` and `createClient` — not an integration, middleware, or Astro Actions adapter.
|
|
4
|
+
|
|
5
|
+
Agent skill: [`skills/ts-pf-astro/`](skills/ts-pf-astro/). Sync with `npx skills experimental_sync -y`.
|
|
6
|
+
|
|
7
|
+
The package root (`@ts-pf/astro`) exports the client helpers (`createAstroClient`, `withBase`). Server helpers are `@ts-pf/astro/server`. Requires **astro >= 4.9.0**. Pass `import.meta.env.BASE_URL` through `withBase` from app source. This package does not read it.
|
|
8
|
+
|
|
9
|
+
## Endpoint
|
|
10
|
+
|
|
11
|
+
`src/pages/rpc/[...path].ts`. `ALL` so CORS `OPTIONS` and non-POST reach `FetchHandler`. `prerender = false` for on-demand routes when output is static. An adapter is required for those routes. `output: 'server'` already renders on demand. Do not put other pages under `src/pages/rpc/` (static files win over the rest param).
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createAstroHandler, withBase } from '@ts-pf/astro/server'
|
|
15
|
+
|
|
16
|
+
export const prerender = false
|
|
17
|
+
|
|
18
|
+
export const ALL = createAstroHandler(handler, {
|
|
19
|
+
prefix: withBase(import.meta.env.BASE_URL, '/rpc'),
|
|
20
|
+
context: contextFromAstro,
|
|
21
|
+
})
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Page
|
|
25
|
+
|
|
26
|
+
Same context function as the endpoint. Pass `{ signal: Astro.request.signal }` on a call when the page should cancel with the request. Set cookies on the `AstroCookies` object that function put on procedure context (`context.cookies.set`). This package does not copy `Set-Cookie`. Astro writes those cookies onto the response for the route that is rendering: the endpoint `Response` from `createAstroHandler`, and the page response from `createAstroLocalClient`.
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { createAstroLocalClient } from '@ts-pf/astro/server'
|
|
30
|
+
|
|
31
|
+
const caller = await createAstroLocalClient(app, Astro, contextFromAstro)
|
|
32
|
+
const planet = await caller.planet.find({ id: 1 })
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Browser
|
|
36
|
+
|
|
37
|
+
Import the contract with `import type`. Do not import `@ts-pf/astro/server` or the implementer from a client island or `<script>`. `@ts-pf/swr` works on this client for React islands.
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { createAstroClient, withBase } from '@ts-pf/astro/client'
|
|
41
|
+
import type { contract } from './contract'
|
|
42
|
+
|
|
43
|
+
export const client = createAstroClient<typeof contract>({
|
|
44
|
+
prefix: withBase(import.meta.env.BASE_URL, '/rpc'),
|
|
45
|
+
})
|
|
46
|
+
```
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type CallInterceptor, type CallPlugin } from '@ts-pf/client';
|
|
2
|
+
import { FetchLink } from '@ts-pf/client-http';
|
|
3
|
+
import type { ContractClient } from '@ts-pf/contract';
|
|
4
|
+
type FetchLinkOptions = ConstructorParameters<typeof FetchLink>[0];
|
|
5
|
+
export declare function createAstroClient<T>(opts: {
|
|
6
|
+
prefix: string;
|
|
7
|
+
headers?: NonNullable<FetchLinkOptions['headers']>;
|
|
8
|
+
fetch?: NonNullable<FetchLinkOptions['fetch']>;
|
|
9
|
+
codec?: NonNullable<FetchLinkOptions['codec']>;
|
|
10
|
+
plugins?: readonly CallPlugin[];
|
|
11
|
+
interceptors?: readonly CallInterceptor[];
|
|
12
|
+
}): ContractClient<T>;
|
|
13
|
+
export { withBase } from './with-base.js';
|
|
14
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,UAAU,EAEhB,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAC9C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAErD,KAAK,gBAAgB,GAAG,qBAAqB,CAAC,OAAO,SAAS,CAAC,CAAC,CAAC,CAAC,CAAA;AAElE,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,IAAI,EAAE;IACzC,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,WAAW,CAAC,gBAAgB,CAAC,SAAS,CAAC,CAAC,CAAA;IAClD,KAAK,CAAC,EAAE,WAAW,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAA;IAC9C,KAAK,CAAC,EAAE,WAAW,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAA;IAC9C,OAAO,CAAC,EAAE,SAAS,UAAU,EAAE,CAAA;IAC/B,YAAY,CAAC,EAAE,SAAS,eAAe,EAAE,CAAA;CAC1C,GAAG,cAAc,CAAC,CAAC,CAAC,CAIpB;AAiDD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { createClient, } from '@ts-pf/client';
|
|
2
|
+
import { FetchLink } from '@ts-pf/client-http';
|
|
3
|
+
export function createAstroClient(opts) {
|
|
4
|
+
const link = new FetchLink(fetchLinkOptions(opts));
|
|
5
|
+
const clientOpts = clientOptions(opts);
|
|
6
|
+
return clientOpts ? createClient(link, clientOpts) : createClient(link);
|
|
7
|
+
}
|
|
8
|
+
function fetchLinkOptions(opts) {
|
|
9
|
+
const linkOpts = { url: opts.prefix };
|
|
10
|
+
if (opts.headers !== undefined) {
|
|
11
|
+
linkOpts.headers = opts.headers;
|
|
12
|
+
}
|
|
13
|
+
if (opts.fetch !== undefined) {
|
|
14
|
+
linkOpts.fetch = opts.fetch;
|
|
15
|
+
}
|
|
16
|
+
if (opts.codec !== undefined) {
|
|
17
|
+
linkOpts.codec = opts.codec;
|
|
18
|
+
}
|
|
19
|
+
return linkOpts;
|
|
20
|
+
}
|
|
21
|
+
function clientOptions(opts) {
|
|
22
|
+
const clientOpts = {};
|
|
23
|
+
if (opts.plugins !== undefined) {
|
|
24
|
+
clientOpts.plugins = opts.plugins;
|
|
25
|
+
}
|
|
26
|
+
if (opts.interceptors !== undefined) {
|
|
27
|
+
clientOpts.interceptors = opts.interceptors;
|
|
28
|
+
}
|
|
29
|
+
if (clientOpts.plugins === undefined &&
|
|
30
|
+
clientOpts.interceptors === undefined) {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
return clientOpts;
|
|
34
|
+
}
|
|
35
|
+
export { withBase } from './with-base.js';
|
|
36
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,YAAY,GACb,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAK9C,MAAM,UAAU,iBAAiB,CAAI,IAOpC;IACC,MAAM,IAAI,GAAG,IAAI,SAAS,CAAC,gBAAgB,CAAC,IAAI,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}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA"}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ContractClient } from '@ts-pf/contract';
|
|
2
|
+
import { type CallInterceptor, type CallPlugin, type ImplementedRouter, type ProcedureValidation } from '@ts-pf/server';
|
|
3
|
+
import type { FetchHandler } from '@ts-pf/server-http';
|
|
4
|
+
import type { APIContext } from 'astro';
|
|
5
|
+
export type AstroHandlerOptions<TCtx> = {
|
|
6
|
+
prefix: string;
|
|
7
|
+
context: TCtx | ((astro: APIContext) => TCtx | Promise<TCtx>);
|
|
8
|
+
};
|
|
9
|
+
export type AstroLocalClientOptions = {
|
|
10
|
+
interceptors?: readonly CallInterceptor[];
|
|
11
|
+
plugins?: readonly CallPlugin[];
|
|
12
|
+
validation?: ProcedureValidation;
|
|
13
|
+
};
|
|
14
|
+
export declare function createAstroHandler<TCtx>(handler: FetchHandler<TCtx>, options: AstroHandlerOptions<TCtx>): (context: APIContext) => Promise<Response>;
|
|
15
|
+
export declare function createAstroLocalClient<TApp, TAstro extends {
|
|
16
|
+
request: Request;
|
|
17
|
+
}, TCtx>(app: ImplementedRouter<TApp>, astro: TAstro, context: TCtx | ((astro: TAstro) => TCtx | Promise<TCtx>), options?: AstroLocalClientOptions): Promise<ContractClient<TApp>>;
|
|
18
|
+
export { withBase } from './with-base.js';
|
|
19
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @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BAAuB,GAChC,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,CAG/B;AA4CD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA"}
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { createLocalClient, } from '@ts-pf/server';
|
|
2
|
+
export function createAstroHandler(handler, options) {
|
|
3
|
+
return async (astro) => {
|
|
4
|
+
const context = await resolveContext(options.context, astro);
|
|
5
|
+
const result = await handler.handle(astro.request, {
|
|
6
|
+
prefix: options.prefix,
|
|
7
|
+
context,
|
|
8
|
+
});
|
|
9
|
+
if (!result.matched) {
|
|
10
|
+
return new Response('Not Found', { status: 404 });
|
|
11
|
+
}
|
|
12
|
+
return result.response;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export async function createAstroLocalClient(app, astro, context, options) {
|
|
16
|
+
const resolved = await resolveContext(context, astro);
|
|
17
|
+
return createLocalClient(app, localClientOptions(resolved, options));
|
|
18
|
+
}
|
|
19
|
+
// A function is the context factory. Context values are plain data.
|
|
20
|
+
async function resolveContext(context, astro) {
|
|
21
|
+
if (typeof context === 'function') {
|
|
22
|
+
const factory = context;
|
|
23
|
+
return factory(astro);
|
|
24
|
+
}
|
|
25
|
+
return context;
|
|
26
|
+
}
|
|
27
|
+
function localClientOptions(context, options) {
|
|
28
|
+
const opts = { context };
|
|
29
|
+
if (!options) {
|
|
30
|
+
return opts;
|
|
31
|
+
}
|
|
32
|
+
if (options.interceptors !== undefined) {
|
|
33
|
+
opts.interceptors = options.interceptors;
|
|
34
|
+
}
|
|
35
|
+
if (options.plugins !== undefined) {
|
|
36
|
+
opts.plugins = options.plugins;
|
|
37
|
+
}
|
|
38
|
+
if (options.validation !== undefined) {
|
|
39
|
+
opts.validation = options.validation;
|
|
40
|
+
}
|
|
41
|
+
return opts;
|
|
42
|
+
}
|
|
43
|
+
export { withBase } from './with-base.js';
|
|
44
|
+
//# sourceMappingURL=server.js.map
|
|
@@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IAAI,GAKN,EAAE,OAAO,EAAE,CAAA;IACf,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO,IAAI,CAAA;IACb,CAAC;IACD,IAAI,OAAO,CAAC,YAAY,KAAK,SAAS,EAAE,CAAC;QACvC,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAA;IAC1C,CAAC;IACD,IAAI,OAAO,CAAC,OAAO,KAAK,SAAS,EAAE,CAAC;QAClC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAA;IAChC,CAAC;IACD,IAAI,OAAO,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;QACrC,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAA;IACtC,CAAC;IACD,OAAO,IAAI,CAAA;AACb,CAAC;AAED,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"with-base.d.ts","sourceRoot":"","sources":["../src/with-base.ts"],"names":[],"mappings":"AAAA,mFAAmF;AACnF,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAWhE"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Join an Astro base path with a route prefix. Simple join; no base detection. */
|
|
2
|
+
export function withBase(baseUrl, prefix) {
|
|
3
|
+
const base = baseUrl.replace(/\/+$/, '');
|
|
4
|
+
let path = prefix;
|
|
5
|
+
if (path.length > 0 && !path.startsWith('/')) {
|
|
6
|
+
path = `/${path}`;
|
|
7
|
+
}
|
|
8
|
+
const joined = `${base}${path}`;
|
|
9
|
+
if (joined.length === 0) {
|
|
10
|
+
return '/';
|
|
11
|
+
}
|
|
12
|
+
return joined;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=with-base.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"with-base.js","sourceRoot":"","sources":["../src/with-base.ts"],"names":[],"mappings":"AAAA,mFAAmF;AACnF,MAAM,UAAU,QAAQ,CAAC,OAAe,EAAE,MAAc;IACtD,MAAM,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;IACxC,IAAI,IAAI,GAAG,MAAM,CAAA;IACjB,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QAC7C,IAAI,GAAG,IAAI,IAAI,EAAE,CAAA;IACnB,CAAC;IACD,MAAM,MAAM,GAAG,GAAG,IAAI,GAAG,IAAI,EAAE,CAAA;IAC/B,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACxB,OAAO,GAAG,CAAA;IACZ,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ts-pf/astro",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Opt-in Astro endpoint and in-process page caller for ts-pf",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ts-pf",
|
|
7
|
+
"typescript",
|
|
8
|
+
"rpc",
|
|
9
|
+
"typesafe",
|
|
10
|
+
"typed-rpc",
|
|
11
|
+
"api",
|
|
12
|
+
"contract-first",
|
|
13
|
+
"astro"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/crobinson42/ts-pf.git",
|
|
19
|
+
"directory": "packages/astro"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://github.com/crobinson42/ts-pf/tree/main/packages/astro#readme",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/crobinson42/ts-pf/issues"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./client": {
|
|
33
|
+
"types": "./dist/client.d.ts",
|
|
34
|
+
"import": "./dist/client.js"
|
|
35
|
+
},
|
|
36
|
+
"./server": {
|
|
37
|
+
"types": "./dist/server.d.ts",
|
|
38
|
+
"import": "./dist/server.js"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"files": [
|
|
45
|
+
"dist",
|
|
46
|
+
"skills"
|
|
47
|
+
],
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "tsc -p tsconfig.build.json",
|
|
50
|
+
"test": "vitest run",
|
|
51
|
+
"type-check": "tsc --noEmit",
|
|
52
|
+
"lint": "biome check src tests"
|
|
53
|
+
},
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"@ts-pf/client": "0.1.5",
|
|
56
|
+
"@ts-pf/client-http": "0.1.5",
|
|
57
|
+
"@ts-pf/server": "0.3.1",
|
|
58
|
+
"@ts-pf/server-http": "0.2.1"
|
|
59
|
+
},
|
|
60
|
+
"peerDependencies": {
|
|
61
|
+
"astro": ">=4.9.0"
|
|
62
|
+
},
|
|
63
|
+
"devDependencies": {
|
|
64
|
+
"astro": "^5.16.0",
|
|
65
|
+
"zod": "^4.0.0"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: ts-pf-astro
|
|
3
|
+
description: Use when serving a ts-pf API from an Astro endpoint or calling it in a page or island. Triggers: @ts-pf/astro, createAstroHandler, createAstroLocalClient, createAstroClient, withBase.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# @ts-pf/astro
|
|
7
|
+
|
|
8
|
+
Opt-in Astro endpoint and in-process page caller. Thin shell around `FetchHandler` and `createClient`. Not an integration, middleware mount, or Astro Actions adapter.
|
|
9
|
+
|
|
10
|
+
Install: `npm i @ts-pf/astro` (peer **astro >= 4.9.0**). The app already has `@ts-pf/server`, `@ts-pf/server-http`, `@ts-pf/client`, and `@ts-pf/client-http`.
|
|
11
|
+
|
|
12
|
+
Link for agents: `npx skills experimental_sync -y`
|
|
13
|
+
|
|
14
|
+
## Do
|
|
15
|
+
|
|
16
|
+
Endpoint `src/pages/rpc/[...path].ts`. Export `ALL` so CORS `OPTIONS` and non-POST reach `FetchHandler`. Set `prerender = false` when output is static. An adapter is required for on-demand routes. `output: 'server'` already renders on demand. Do not put other pages under `src/pages/rpc/` (static files win over the rest param).
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { createAstroHandler, withBase } from '@ts-pf/astro/server'
|
|
20
|
+
|
|
21
|
+
export const prerender = false
|
|
22
|
+
|
|
23
|
+
export const ALL = createAstroHandler(handler, {
|
|
24
|
+
prefix: withBase(import.meta.env.BASE_URL, '/rpc'),
|
|
25
|
+
context: contextFromAstro,
|
|
26
|
+
})
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Page frontmatter. Same context function as the endpoint when the `Astro` global and `APIContext` are structurally compatible. Pass `{ signal: Astro.request.signal }` on a call when the page should cancel with the request. Set cookies on the `AstroCookies` object the context function put on procedure context (`context.cookies.set`). This package does not copy `Set-Cookie`. Astro writes those cookies onto the response for the route that is rendering: the endpoint `Response` from `createAstroHandler`, and the page response from `createAstroLocalClient`.
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { createAstroLocalClient } from '@ts-pf/astro/server'
|
|
33
|
+
|
|
34
|
+
const caller = await createAstroLocalClient(app, Astro, contextFromAstro)
|
|
35
|
+
const planet = await caller.planet.find({ id: 1 })
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Browser or island module. Import the contract with `import type`. Do not import the implementer.
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { createAstroClient, withBase } from '@ts-pf/astro/client'
|
|
42
|
+
import type { contract } from './contract'
|
|
43
|
+
|
|
44
|
+
export const client = createAstroClient<typeof contract>({
|
|
45
|
+
prefix: withBase(import.meta.env.BASE_URL, '/rpc'),
|
|
46
|
+
})
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
`@ts-pf/astro` (root) exports the same client helpers. `@ts-pf/swr` works on that client for React islands.
|
|
50
|
+
|
|
51
|
+
## API
|
|
52
|
+
|
|
53
|
+
- `createAstroHandler`
|
|
54
|
+
- `createAstroLocalClient`
|
|
55
|
+
- `createAstroClient`
|
|
56
|
+
- `withBase`
|
|
57
|
+
- `AstroHandlerOptions`
|
|
58
|
+
- `AstroLocalClientOptions`
|
|
59
|
+
|
|
60
|
+
`createAstroHandler`, `createAstroLocalClient`, `AstroHandlerOptions`, and `AstroLocalClientOptions` are `@ts-pf/astro/server`. `createAstroClient` and `withBase` are `@ts-pf/astro` and `@ts-pf/astro/client`.
|
|
61
|
+
|
|
62
|
+
## Pair with
|
|
63
|
+
|
|
64
|
+
- `ts-pf-server-http`
|
|
65
|
+
- `ts-pf-client-http`
|
|
66
|
+
- `ts-pf-server`
|
|
67
|
+
- `ts-pf-client`
|
|
68
|
+
|
|
69
|
+
## Don't
|
|
70
|
+
|
|
71
|
+
- Import `@ts-pf/astro/server` from a client island or `<script>`.
|
|
72
|
+
- Import the implementer module from a client module.
|
|
73
|
+
- Read `import.meta.env.BASE_URL` inside the library (pass it through `withBase` from app source).
|
|
74
|
+
- Mount procedures as Astro Actions.
|
|
75
|
+
- Add an Astro integration that injects the route.
|
|
76
|
+
- Serve OpenAPI or `catalog.json` from the RPC route.
|
|
77
|
+
- Use `trailingSlash: 'always'` or `'never'` when request bodies are streams (`StreamCodec` input). Default `'ignore'` is the match. JSON POST under always/never is a 308 that fetch can follow; a `ReadableStream` body cannot be replayed.
|
|
78
|
+
- Consume `request.body` in the context factory before the handler runs.
|
|
79
|
+
- Turn off `security.checkOrigin`. It stays on. It covers `multipart/form-data` (file uploads via `MultipartCodec`), not `application/json` or `application/jsonl`.
|