@assembly-js/app-foundation 0.2.3 → 0.2.4

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/CHANGELOG.md CHANGED
@@ -17,6 +17,9 @@ bullets ship. Write `[fix]`, `[behavior]` and `[action]` bullets for the app's o
17
17
  A patch release never changes what an app's build requires; a minor release may add or change a
18
18
  `bin/check-*.mjs` script. CI fails a PR that changes a check script without a minor bump over main.
19
19
 
20
+ ## 0.2.4
21
+ - [new] Add `@assembly-js/app-foundation/codegen`, which turns another app's API description into a typed client the app can call by verb and path, and `appProxyOptions` in `/server`, which that client uses to reach the other app through the platform. Nothing in the app changes until a client is generated for it.
22
+
20
23
  ## 0.2.3
21
24
  - [new] The client-call scan now treats a client's own notification reads (list, mark read, mark unread) as safe, matching the routes the platform opens to clients. Nothing in the app changes.
22
25
 
@@ -1,4 +1,4 @@
1
- // Every name `@assembly-js/app-foundation/server` and `/client` export that is NOT a way to
1
+ // Every name `@assembly-js/app-foundation/server`, `/client` and `/codegen` export that is NOT a way to
2
2
  // obtain an Assembly client or request context for the person using the app. The scan
3
3
  // follows viewer clients only from the sources it knows (SDK_FACTORIES, afterAssembly,
4
4
  // getRequestContext in scan-client-calls.mjs); an import from those entry points that is in
@@ -18,15 +18,15 @@
18
18
  // renews the bearer cookie; it never hands out a client.
19
19
  export const NON_CLIENT_EXPORTS = new Set([
20
20
  'APP_CSRF_HEADER', 'AUTH_UNAVAILABLE_CODE', 'ActionContext', 'ActionError',
21
- 'ActionErrorStatus', 'AirtableFieldSchema', 'AirtableFormattedField',
21
+ 'ActionErrorStatus', 'AirtableFieldSchema', 'AppProxyCaller', 'AppProxyOptions', 'AirtableFormattedField',
22
22
  'AirtableFormattedRecord', 'AirtableRecord', 'AirtableSchemaResult', 'AirtableTableSchema',
23
23
  'AssemblyProxyForwardContext', 'AssemblyProxyOptions', 'AuthUnavailableError',
24
24
  'BEARER_COOKIE_NAME', 'BEARER_HEADER_NAME', 'BEARER_IN_BODY_HEADER', 'BEARER_TTL_MS',
25
25
  'CLIENT_BEARER_HEADER', 'CLIENT_FLAGS_COOKIE_NAME', 'COOKIE_OUTCOME_EVENT',
26
26
  'COOKIE_PROBE_NAME', 'Caller', 'ClearClientNotificationsInput', 'ClientAccessScope',
27
- 'ConnectionStatus', 'ConnectorError', 'ConnectorErrorCode', 'CookieOutcome', 'CronHandler',
27
+ 'ConnectionStatus', 'ConnectorError', 'ContractDocumentError', 'ConnectorErrorCode', 'CookieOutcome', 'CronHandler',
28
28
  'DEFAULT_LABELS', 'EXCHANGE_REJECTION_HEADER', 'ExchangeRejectedViewer', 'ExchangedSession',
29
- 'FormatAirtableRecordsOpts', 'GmailSendArgs', 'GmailSendResult', 'IntegrationClient',
29
+ 'FormatAirtableRecordsOpts', 'GenerateAppClientInput', 'GeneratedAppClient', 'GmailSendArgs', 'GmailSendResult', 'IntegrationClient',
30
30
  'InternalUserAccess', 'LEGACY_APP_IDS', 'MintFailure', 'MintOutcome', 'MintSessionOptions', 'MintedAppBearer',
31
31
  'MintedBearer', 'NETWORK_CAPTURE_MESSAGE_TYPE', 'NetworkCaptureHeaderFingerprint',
32
32
  'NetworkCaptureRecord', 'PAINTED_BEACON_EVENT', 'PAINTED_BEACON_PATH',
@@ -37,13 +37,13 @@ export const NON_CLIENT_EXPORTS = new Set([
37
37
  'SlackPostMessageResult', 'TOKEN_EXPIRED_HEADER', 'TokenInvalidError', 'TokenPayload',
38
38
  'TtlCache', 'VERIFY_OUTCOME_EVENT', 'VERIFY_PATH', 'VIEWER_BEARER_HEADER',
39
39
  'VerifiedIdentity', 'VerifyResult', 'Viewer', 'WorkspaceLabels', 'authUnavailableResponse',
40
- 'automationActionRoute', 'badRequest', 'bootstrapPortalOrigin', 'buildMimeMessage',
40
+ 'appProxyOptions', 'automationActionRoute', 'badRequest', 'bootstrapPortalOrigin', 'buildMimeMessage',
41
41
  'canAccessClient', 'canAccessCompanies', 'classifyExchangeFailure',
42
42
  'clearClientNotifications', 'collectHeaders', 'collectXhrResponseHeaders',
43
43
  'cookielessReexchangeHeaders', 'cookiesAreBlocked', 'createAssemblyProxy',
44
44
  'createAssemblyReexchangeHandler', 'cronRoute',
45
45
  'decodeBearer', 'expandClientsByCompany', 'extractPdfText', 'fetchSessionFlags',
46
- 'findResource', 'formatAirtableRecords', 'getAirtableTableSchema', 'getCapturedRecords',
46
+ 'findResource', 'formatAirtableRecords', 'generateAppClient', 'getAirtableTableSchema', 'getCapturedRecords',
47
47
  'getClientAccessScope', 'getCookielessBearer', 'getDownloadUrl', 'getInstalledAppIds', 'getIntegration',
48
48
  'getInternalUserAccess', 'getLabels', 'getSessionIdentity', 'getUploadUrl',
49
49
  'getVerifiedIdentity', 'gmailSendEmail', 'handlePaintedBeacon', 'handleRouteError',
@@ -102,5 +102,22 @@ export declare function getOfflineAssembly(): Promise<AssemblyAPI>;
102
102
  * in the same request reuses that bearer.
103
103
  */
104
104
  export declare function getInstallerAssembly(): Promise<AssemblyAPI>;
105
+ export type AppProxyCaller = 'viewer' | 'installer';
106
+ export interface AppProxyOptions {
107
+ baseUrl: string;
108
+ fetch: (input: Request) => Promise<Response>;
109
+ }
110
+ /**
111
+ * Options for calling another app's API through the platform, shaped for an
112
+ * `openapi-fetch` client: the generated `client.ts` passes them straight to
113
+ * `createClient`. The bearer is resolved on every call, so a client built at
114
+ * module scope still authenticates as the current request.
115
+ *
116
+ * `installer` calls as this app, with the same per-request mint as
117
+ * getInstallerAssembly. `viewer` forwards the signed-in user's verified bearer,
118
+ * which the platform refuses with `viewer_not_allowed` until it can prove which
119
+ * person a cross-app call is for.
120
+ */
121
+ export declare function appProxyOptions(appId: string, caller: AppProxyCaller): AppProxyOptions;
105
122
  export declare function getAssemblyForRequest(req: NextRequest): Promise<AssemblyAPI | NextResponse>;
106
123
  export {};
package/dist/assembly.js CHANGED
@@ -1,4 +1,4 @@
1
- import { assemblyApi } from '@assembly-js/node-sdk';
1
+ import { assemblyApi, OpenAPI } from '@assembly-js/node-sdk';
2
2
  import { cookies, headers } from 'next/headers';
3
3
  import { after, NextResponse } from 'next/server';
4
4
  import { BEARER_COOKIE_NAME, BEARER_HEADER_NAME, decodeBearer, isSessionExchangeEnabled, TOKEN_EXPIRED_HEADER, } from './session-exchange.js';
@@ -332,8 +332,8 @@ export async function getOfflineAssembly() {
332
332
  // for the request, so keying on it shares a mint across the page without
333
333
  // keeping the bearer after the request ends. A process-wide cache would hand
334
334
  // one viewer's app bearer to the next request.
335
- const installerByRequest = new WeakMap();
336
- async function mintInstallerAssembly() {
335
+ const installerBearerByRequest = new WeakMap();
336
+ async function mintInstallerBearer() {
337
337
  if (!process.env.ASSEMBLY_API_KEY) {
338
338
  throw new Error('getInstallerAssembly: ASSEMBLY_API_KEY is not set — cannot mint an app-scoped bearer');
339
339
  }
@@ -341,18 +341,11 @@ async function mintInstallerAssembly() {
341
341
  if (!minted) {
342
342
  throw new Error('getInstallerAssembly: failed to mint an app-scoped bearer from the app API key');
343
343
  }
344
- return getBearerAssembly(minted.bearer);
344
+ return minted.bearer;
345
345
  }
346
- /**
347
- * App-scoped client for use inside a user request (pages, server actions,
348
- * route handlers). Acts as the installing admin: anything it returns that is
349
- * shown to a client must be filtered by the caller. Unlike getOfflineAssembly,
350
- * a viewer cookie does not make this throw — that is the point of it.
351
- *
352
- * Does not attach the viewer's bearer. Each request mints once; a second call
353
- * in the same request reuses that bearer.
354
- */
355
- export async function getInstallerAssembly() {
346
+ // Shared by getInstallerAssembly and the installer app proxy, so a request that
347
+ // uses both still mints once.
348
+ async function getInstallerBearer() {
356
349
  let headerStore;
357
350
  try {
358
351
  headerStore = await headers();
@@ -361,21 +354,62 @@ export async function getInstallerAssembly() {
361
354
  headerStore = undefined;
362
355
  }
363
356
  if (!headerStore)
364
- return mintInstallerAssembly();
365
- const existing = installerByRequest.get(headerStore);
357
+ return mintInstallerBearer();
358
+ const existing = installerBearerByRequest.get(headerStore);
366
359
  if (existing)
367
360
  return existing;
368
- const pending = mintInstallerAssembly();
369
- installerByRequest.set(headerStore, pending);
361
+ const pending = mintInstallerBearer();
362
+ installerBearerByRequest.set(headerStore, pending);
370
363
  try {
371
364
  return await pending;
372
365
  }
373
366
  catch (err) {
374
367
  // A failed mint must not stick for the rest of the request.
375
- installerByRequest.delete(headerStore);
368
+ installerBearerByRequest.delete(headerStore);
376
369
  throw err;
377
370
  }
378
371
  }
372
+ /**
373
+ * App-scoped client for use inside a user request (pages, server actions,
374
+ * route handlers). Acts as the installing admin: anything it returns that is
375
+ * shown to a client must be filtered by the caller. Unlike getOfflineAssembly,
376
+ * a viewer cookie does not make this throw — that is the point of it.
377
+ *
378
+ * Does not attach the viewer's bearer. Each request mints once; a second call
379
+ * in the same request reuses that bearer.
380
+ */
381
+ export async function getInstallerAssembly() {
382
+ return getBearerAssembly(await getInstallerBearer());
383
+ }
384
+ /**
385
+ * Options for calling another app's API through the platform, shaped for an
386
+ * `openapi-fetch` client: the generated `client.ts` passes them straight to
387
+ * `createClient`. The bearer is resolved on every call, so a client built at
388
+ * module scope still authenticates as the current request.
389
+ *
390
+ * `installer` calls as this app, with the same per-request mint as
391
+ * getInstallerAssembly. `viewer` forwards the signed-in user's verified bearer,
392
+ * which the platform refuses with `viewer_not_allowed` until it can prove which
393
+ * person a cross-app call is for.
394
+ */
395
+ export function appProxyOptions(appId, caller) {
396
+ const origin = (trustedBaseUrl ?? OpenAPI.BASE).replace(/\/+$/, '');
397
+ return {
398
+ baseUrl: `${origin}/v1/apps/${encodeURIComponent(appId)}/api`,
399
+ fetch: async (input) => {
400
+ const bearer = caller === 'installer' ? await getInstallerBearer() : await requireViewerBearer();
401
+ const request = new Request(input);
402
+ request.headers.set('Authorization', `Bearer ${bearer}`);
403
+ return fetch(request);
404
+ },
405
+ };
406
+ }
407
+ async function requireViewerBearer() {
408
+ const viewer = await resolveRequestViewer();
409
+ if (!viewer)
410
+ throw new TokenInvalidError('Assembly bearer is required');
411
+ return viewer.bearer;
412
+ }
379
413
  // Returns a request-scoped SDK client authenticated for the current user.
380
414
  //
381
415
  // Exchange enabled: bearer-only. A bearer the platform verifies (proxy header or
@@ -0,0 +1,14 @@
1
+ export declare class ContractDocumentError extends Error {
2
+ constructor(message: string);
3
+ }
4
+ export interface GenerateAppClientInput {
5
+ appId: string;
6
+ document: unknown;
7
+ contractHash: string;
8
+ }
9
+ export interface GeneratedAppClient {
10
+ typesDts: string;
11
+ clientTs: string;
12
+ readmeMd: string;
13
+ }
14
+ export declare function generateAppClient({ appId, document, contractHash, }: GenerateAppClientInput): Promise<GeneratedAppClient>;
@@ -0,0 +1,178 @@
1
+ // Generates a typed client for another app's API from its OpenAPI contract.
2
+ // The sandbox runs this with plain node, so nothing here may import next/* or
3
+ // server-only, and it never touches the network or the app's repo.
4
+ import { createRequire } from 'node:module';
5
+ import openapiTS, { astToString } from 'openapi-typescript';
6
+ // The package version is the generator version: the same document and the same
7
+ // foundation version always produce byte-identical files.
8
+ const FOUNDATION_VERSION = createRequire(import.meta.url)('../package.json').version;
9
+ const HTTP_METHODS = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'];
10
+ // Contracts are written as served (`/api/...`) while the client's base URL
11
+ // already ends in `/api`. Same rule as contractTemplate in bin/check-contract-routes.mjs.
12
+ const API_PREFIX = '/api';
13
+ export class ContractDocumentError extends Error {
14
+ constructor(message) {
15
+ super(message);
16
+ this.name = 'ContractDocumentError';
17
+ }
18
+ }
19
+ function contractTemplate(contractPath) {
20
+ let p = contractPath.replace(/\/+$/, '');
21
+ if (p === API_PREFIX || p.startsWith(`${API_PREFIX}/`))
22
+ p = p.slice(API_PREFIX.length);
23
+ return p || '/';
24
+ }
25
+ const isObject = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
26
+ function forEachRef(node, at, visit) {
27
+ if (Array.isArray(node)) {
28
+ node.forEach((child, i) => forEachRef(child, `${at}/${i}`, visit));
29
+ return;
30
+ }
31
+ if (!isObject(node))
32
+ return;
33
+ if (typeof node.$ref === 'string')
34
+ visit(node, at);
35
+ for (const [key, value] of Object.entries(node))
36
+ forEachRef(value, `${at}/${key}`, visit);
37
+ }
38
+ const pointerToken = (key) => key.replace(/~/g, '~0').replace(/\//g, '~1');
39
+ function normalizeDocument(document) {
40
+ if (!isObject(document))
41
+ throw new ContractDocumentError('contract document must be a JSON object');
42
+ if (typeof document.openapi !== 'string') {
43
+ throw new ContractDocumentError('contract document is missing "openapi"');
44
+ }
45
+ if (!document.openapi.startsWith('3.')) {
46
+ throw new ContractDocumentError(`contract document "openapi" must be 3.x, got "${document.openapi}"`);
47
+ }
48
+ if (!isObject(document.paths))
49
+ throw new ContractDocumentError('contract document is missing "paths"');
50
+ let external;
51
+ forEachRef(document, '#', (holder, at) => {
52
+ if (!external && !holder.$ref.startsWith('#'))
53
+ external = `${at}/$ref`;
54
+ });
55
+ if (external)
56
+ throw new ContractDocumentError(`contract document has an external "$ref" at ${external}`);
57
+ const copy = structuredClone(document);
58
+ const paths = {};
59
+ const renamed = new Map();
60
+ for (const [documented, item] of Object.entries(copy.paths)) {
61
+ const template = contractTemplate(documented);
62
+ if (template in paths) {
63
+ throw new ContractDocumentError(`contract document "paths" has two entries for ${template}`);
64
+ }
65
+ paths[template] = item;
66
+ if (template !== documented) {
67
+ renamed.set(`#/paths/${pointerToken(documented)}`, `#/paths/${pointerToken(template)}`);
68
+ }
69
+ }
70
+ // A $ref into a renamed path item would otherwise point at a key that no longer exists.
71
+ forEachRef(copy, '#', (holder) => {
72
+ for (const [from, to] of renamed) {
73
+ if (holder.$ref === from || holder.$ref.startsWith(`${from}/`)) {
74
+ holder.$ref = to + holder.$ref.slice(from.length);
75
+ break;
76
+ }
77
+ }
78
+ });
79
+ return { ...copy, paths };
80
+ }
81
+ // openapi-typescript names an `operations` entry after each operationId; without
82
+ // it the operation is inlined under its path, so the (verb, path) pair stays the
83
+ // only identity the types expose.
84
+ function stripOperationIds(paths) {
85
+ for (const item of Object.values(paths)) {
86
+ if (!isObject(item))
87
+ continue;
88
+ for (const method of HTTP_METHODS) {
89
+ const op = item[method];
90
+ if (isObject(op))
91
+ delete op.operationId;
92
+ }
93
+ }
94
+ }
95
+ const PATHS_POINTER = '#/paths/';
96
+ const decodePointerToken = (token) => token.replace(/~1/g, '/').replace(/~0/g, '~');
97
+ // A path item that is a $ref into another path still serves that path's operations under its own key.
98
+ function resolvePathItem(paths, item) {
99
+ if (!isObject(item) || typeof item.$ref !== 'string' || !item.$ref.startsWith(PATHS_POINTER))
100
+ return item;
101
+ return paths[decodePointerToken(item.$ref.slice(PATHS_POINTER.length))];
102
+ }
103
+ function operationsOf(paths) {
104
+ const ops = [];
105
+ for (const [path, entry] of Object.entries(paths)) {
106
+ const item = resolvePathItem(paths, entry);
107
+ if (!isObject(item))
108
+ continue;
109
+ for (const method of HTTP_METHODS) {
110
+ const op = item[method];
111
+ if (!isObject(op))
112
+ continue;
113
+ ops.push({
114
+ method: method.toUpperCase(),
115
+ path,
116
+ operationId: typeof op.operationId === 'string' ? op.operationId : undefined,
117
+ summary: typeof op.summary === 'string' ? op.summary : undefined,
118
+ });
119
+ }
120
+ }
121
+ return ops;
122
+ }
123
+ const header = (contractHash) => `// Generated by @assembly-js/app-foundation ${FOUNDATION_VERSION} from contract ${contractHash}. Do not edit.\n`;
124
+ // Fixed apart from the app id, so every generated client behaves the same way
125
+ // and all auth mechanics stay in the package.
126
+ function renderClient(appId) {
127
+ return `// Generated by @assembly-js/app-foundation ${FOUNDATION_VERSION}. Do not edit.
128
+ import createClient from 'openapi-fetch';
129
+ import { appProxyOptions } from '@assembly-js/app-foundation/server';
130
+ import type { paths } from './types.js';
131
+
132
+ export const APP_ID = ${JSON.stringify(appId)};
133
+
134
+ // Calls as this app. Use it from pages, server actions, routes, cron and webhooks.
135
+ export const installerClient = createClient<paths>(appProxyOptions(APP_ID, 'installer'));
136
+
137
+ // Calls as the signed-in person. Refused with viewer_not_allowed for now.
138
+ export const viewerClient = createClient<paths>(appProxyOptions(APP_ID, 'viewer'));
139
+ `;
140
+ }
141
+ const cell = (s) => s.replace(/\\/g, '\\\\').replace(/\|/g, '\\|').replace(/\r?\n/g, ' ');
142
+ function renderReadme(appId, contractHash, paths) {
143
+ const rows = operationsOf(paths).map((op) => `| \`${op.method}\` | \`${cell(op.path)}\` | ${cell(op.operationId ?? '')} | ${cell(op.summary ?? '')} |`);
144
+ return `# Client for app \`${appId}\`
145
+
146
+ Generated by \`@assembly-js/app-foundation\` ${FOUNDATION_VERSION} from contract \`${contractHash}\`. Do not edit these files; they are regenerated when the contract changes.
147
+
148
+ Call an operation by its verb and path, exactly as listed below. The path and its parameters are type-checked against the contract.
149
+
150
+ \`\`\`ts
151
+ import { installerClient } from './client.js';
152
+
153
+ const { data, error } = await installerClient.GET('/projects/{id}', {
154
+ params: { path: { id } },
155
+ });
156
+ \`\`\`
157
+
158
+ - \`installerClient\` calls as this app. Use it from pages, server actions, routes, cron and webhooks, and filter anything you show to a client.
159
+ - \`viewerClient\` calls as the signed-in person. The platform refuses it with \`viewer_not_allowed\` until it can prove which person the call is for, so use \`installerClient\` for now.
160
+
161
+ ## Operations
162
+
163
+ | Verb | Path | Operation | Summary |
164
+ |---|---|---|---|
165
+ ${rows.join('\n')}
166
+ `;
167
+ }
168
+ export async function generateAppClient({ appId, document, contractHash, }) {
169
+ const normalized = normalizeDocument(document);
170
+ const readmeMd = renderReadme(appId, contractHash, normalized.paths);
171
+ stripOperationIds(normalized.paths);
172
+ const ast = await openapiTS(normalized, { silent: true });
173
+ return {
174
+ typesDts: header(contractHash) + astToString(ast),
175
+ clientTs: renderClient(appId),
176
+ readmeMd,
177
+ };
178
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@assembly-js/app-foundation",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "Versioned foundation helpers and build context for Assembly generated apps",
5
5
  "type": "module",
6
6
  "files": [
@@ -31,11 +31,15 @@
31
31
  "./next": {
32
32
  "types": "./dist/next.d.ts",
33
33
  "default": "./dist/next.js"
34
+ },
35
+ "./codegen": {
36
+ "types": "./dist/codegen.d.ts",
37
+ "default": "./dist/codegen.js"
34
38
  }
35
39
  },
36
40
  "scripts": {
37
41
  "build": "tsc && tsc-alias && esbuild src/scripts.ts --bundle --platform=node --format=esm --target=node18 --outfile=dist/scripts.js",
38
- "test": "npm run build && bun test --path-ignore-patterns='**/proxy.test.ts' --path-ignore-patterns='**/reexchange.test.ts' && bun test ./src/proxy.test.ts && bun test ./src/reexchange.test.ts && tsc -p tsconfig.replace-rows.json",
42
+ "test": "npm run build && bun test --path-ignore-patterns='**/proxy.test.ts' --path-ignore-patterns='**/reexchange.test.ts' && bun test ./src/proxy.test.ts && bun test ./src/reexchange.test.ts && tsc -p tsconfig.replace-rows.json && node scripts/emit-codegen-fixture.mjs && tsc -p tsconfig.codegen.json",
39
43
  "prepublishOnly": "npm run build && npm run verify:dist && npm run verify:bin",
40
44
  "verify:dist": "node scripts/verify-dist-resolves.mjs",
41
45
  "verify:bin": "node scripts/verify-bin.mjs"
@@ -71,6 +75,8 @@
71
75
  "author": "Assembly",
72
76
  "license": "MIT",
73
77
  "dependencies": {
78
+ "openapi-fetch": "^0.17.0",
79
+ "openapi-typescript": "^7.13.0",
74
80
  "unpdf": "^1.9.1"
75
81
  }
76
82
  }