@hulla/api-astro 0.0.0-stage → 2.0.1

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.
@@ -0,0 +1,33 @@
1
+ import { FetchServerErrorInput } from "@hulla/api/fetch";
2
+ import { Awaitable, ServerAdapter, ServerContextInput, ServerExecutableFor } from "@hulla/api/server";
3
+ import { Contract } from "@hulla/api";
4
+ import { ClientTransport } from "@hulla/api/client";
5
+ import { APIContext, APIRoute } from "astro";
6
+ //#region src/server.d.ts
7
+ type AstroContext = APIContext;
8
+ type AstroRouteHandler = APIRoute;
9
+ type AstroAdapterContext = {
10
+ readonly request: Request;
11
+ readonly astroContext: AstroContext;
12
+ };
13
+ type AstroServerErrorInput = Omit<FetchServerErrorInput<AstroContext>, 'handlerContext'> & {
14
+ readonly astroContext: AstroContext;
15
+ };
16
+ type AstroServerOptions = {
17
+ readonly onError?: ((input: AstroServerErrorInput) => Awaitable<Response | undefined | void>) | undefined;
18
+ };
19
+ type AstroContextInput<ContractType extends Contract = Contract> = ServerContextInput<ContractType> & AstroAdapterContext;
20
+ type AstroAdapter = ServerAdapter<'astro', AstroAdapterContext> & {
21
+ readonly mount: <const ContractType extends Contract, const Context extends object>(implementation: ServerExecutableFor<ContractType, Context, 'astro', AstroAdapterContext>, options?: AstroServerOptions) => AstroRouteHandler;
22
+ };
23
+ /**
24
+ * Creates a zero-network client transport for an Astro page, endpoint, or server island.
25
+ *
26
+ * The explicit context is the current Astro render context. Inside a server island its request URL identifies Astro's
27
+ * internal island endpoint rather than the page that contains the island.
28
+ */
29
+ declare function astroInProcessTransport<const ContractType extends Contract, const Context extends object>(implementation: ServerExecutableFor<ContractType, Context, 'astro', AstroAdapterContext>, astroContext: AstroContext): ClientTransport;
30
+ /** Creates an Astro adapter with native render-context access and endpoint mounting operations. */
31
+ declare function astroAdapter(options?: AstroServerOptions): AstroAdapter;
32
+ //#endregion
33
+ export { type AstroAdapter, type AstroAdapterContext, type AstroContext, type AstroContextInput, type AstroRouteHandler, type AstroServerErrorInput, type AstroServerOptions, astroAdapter, astroInProcessTransport };
package/dist/index.js ADDED
@@ -0,0 +1,78 @@
1
+ import { createAdapterHandler } from "@hulla/api/adapters";
2
+ import { createFetchHandler } from "@hulla/api/fetch";
3
+ import { assertAdapterContext, createServerAdapter, serverContextAdapterId } from "@hulla/api/server";
4
+
5
+ //#region src/server.ts
6
+ /** Creates an Astro API route backed by an @hulla/api server implementation. */
7
+ function createRouteHandler(implementation, options = {}) {
8
+ const handler = createFetchHandler(implementation, {
9
+ contextAdapter: "astro",
10
+ contextInput: (_request, astroContext) => ({ astroContext }),
11
+ ...options.onError === void 0 ? {} : { onError: ({ handlerContext, ...input }) => options.onError?.({
12
+ ...input,
13
+ astroContext: handlerContext
14
+ }) }
15
+ });
16
+ return async (astroContext) => {
17
+ const request = astroContext.request;
18
+ if (request.method !== "HEAD") return handler(request, astroContext);
19
+ const response = await handler(new Request(request, { method: "GET" }), astroContext);
20
+ return new Response(null, response);
21
+ };
22
+ }
23
+ function inProcessResponse(response) {
24
+ return {
25
+ status: response.status,
26
+ headers: response.headers,
27
+ native: response,
28
+ readBody: (kind) => {
29
+ if (response.body.kind !== kind) throw new TypeError(`Astro in-process response body is ${response.body.kind}, but the client selected ${kind}`);
30
+ return response.body.value;
31
+ }
32
+ };
33
+ }
34
+ /**
35
+ * Creates a zero-network client transport for an Astro page, endpoint, or server island.
36
+ *
37
+ * The explicit context is the current Astro render context. Inside a server island its request URL identifies Astro's
38
+ * internal island endpoint rather than the page that contains the island.
39
+ */
40
+ function astroInProcessTransport(implementation, astroContext) {
41
+ assertAdapterContext(implementation.context, "astro");
42
+ const usesAstroContext = serverContextAdapterId(implementation.context) === "astro";
43
+ const dispatch = createAdapterHandler(implementation);
44
+ return async (request) => {
45
+ request.signal?.throwIfAborted();
46
+ return inProcessResponse(await dispatch({
47
+ request: astroContext.request,
48
+ ...usesAstroContext ? { contextInput: {
49
+ request: astroContext.request,
50
+ astroContext
51
+ } } : {},
52
+ method: request.method,
53
+ pathname: request.path,
54
+ headers: request.headers,
55
+ ...request.query === void 0 ? {} : { query: request.query },
56
+ ...request.body === void 0 ? {} : { body: {
57
+ value: request.body.value,
58
+ contentType: request.body.contentType
59
+ } }
60
+ }));
61
+ };
62
+ }
63
+ let astroAdapterValue;
64
+ function createAstroAdapter(defaults) {
65
+ return createServerAdapter("astro", { mount: (implementation, options) => createRouteHandler(implementation, options === void 0 ? defaults : {
66
+ ...defaults,
67
+ ...options
68
+ }) });
69
+ }
70
+ /** Creates an Astro adapter with native render-context access and endpoint mounting operations. */
71
+ function astroAdapter(options) {
72
+ if (options !== void 0) return createAstroAdapter({ ...options });
73
+ astroAdapterValue ??= createAstroAdapter({});
74
+ return astroAdapterValue;
75
+ }
76
+
77
+ //#endregion
78
+ export { astroAdapter, astroInProcessTransport };
package/package.json CHANGED
@@ -1,6 +1,47 @@
1
1
  {
2
2
  "name": "@hulla/api-astro",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "2.0.1",
4
+ "description": "Astro endpoint and server-island integration for @hulla/api",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/hulladev/api.git",
9
+ "directory": "packages/astro"
10
+ },
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "type": "module",
15
+ "sideEffects": false,
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js"
20
+ }
21
+ },
22
+ "publishConfig": {
23
+ "access": "public",
24
+ "registry": "https://registry.npmjs.org/"
25
+ },
26
+ "scripts": {
27
+ "build": "tsdown",
28
+ "clean": "rm -rf dist coverage .astro",
29
+ "test": "bun run build && vitest run",
30
+ "test:coverage": "bun run build && vitest run --coverage",
31
+ "test:watch": "vitest",
32
+ "typecheck": "tsc --noEmit"
33
+ },
34
+ "devDependencies": {
35
+ "@astrojs/node": "^11.0.0",
36
+ "@hulla/api": "2.0.1",
37
+ "astro": "^7.0.0",
38
+ "zod": "^4.4.3"
39
+ },
40
+ "peerDependencies": {
41
+ "@hulla/api": "^2.0.1",
42
+ "astro": "^5.0.0 || ^6.0.0 || ^7.0.0"
43
+ },
44
+ "engines": {
45
+ "node": ">=22"
46
+ }
47
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.