|
| 1 | +# tsed-flow-package-symbols |
| 2 | + |
| 3 | +Directus operation that imports exported symbols of Ts.ED packages from the contractual JSON `https://tsed.dev/api.json` and writes them into the `package_symbols` collection. |
| 4 | + |
| 5 | +## How it works |
| 6 | + |
| 7 | +- The operation fetches `api.json` (or consumes a compatible payload provided by a previous step, see “Webhook/inline payload” below). |
| 8 | +- It iterates `modules["<package-name>"].symbols` and maps each entry to a `package_symbols` record via `src/mappers/mapApiSymbolToDirectus.ts`. |
| 9 | +- The corresponding package is ensured in the `packages` collection (created if missing) with `type: "official"`. |
| 10 | +- Upsert is performed by the symbol `id` coming from `api.json`. If a record already exists, its `versions` field is merged (union) with the new version from `api.json`. |
| 11 | +- Fields mapped per symbol: |
| 12 | + - `name ← symbolName` |
| 13 | + - `type ← symbolType` |
| 14 | + - `doc_url ← origin(api.json) + path` |
| 15 | + - `markdown_url ← markdown_base + path + ".md"` |
| 16 | + - `deprecated ← status includes "deprecated"` |
| 17 | + - `tags ← status without "deprecated"` |
| 18 | + - `versions ← []` then the global `api.json.version` is appended during import |
| 19 | + |
| 20 | +## Build and run locally |
| 21 | + |
| 22 | +1) Install deps at the repo root |
| 23 | +```bash |
| 24 | +corepack enable |
| 25 | +yarn install --immutable |
| 26 | +``` |
| 27 | + |
| 28 | +2) Build all extensions |
| 29 | +```bash |
| 30 | +yarn build |
| 31 | +``` |
| 32 | + |
| 33 | +3) Start Directus (dev) |
| 34 | +```bash |
| 35 | +yarn start:dev |
| 36 | +``` |
| 37 | + |
| 38 | +## Operation configuration (UI) |
| 39 | + |
| 40 | +Directus → Flows → Add operation → “Ts.ED Package Symbols importer”. |
| 41 | + |
| 42 | +Card options: |
| 43 | +- `url` (string) — `api.json` URL. Default in the card: `https://tsed.dev/api.json`. |
| 44 | +- `markdown_url` (string) — base URL where markdown files live. Default in the card: `https://tsed.dev/ai/references/api`. |
| 45 | + |
| 46 | +Notes about defaults: |
| 47 | +- The operation handler also has an internal fallback for `markdown_url` to `https://tsed.dev/ai/references` if the option isn’t provided by the flow. To get the expected final markdown path `…/api/<symbol>.md`, set the card option explicitly to `https://tsed.dev/ai/references/api` (as in the exported flows). |
| 48 | + |
| 49 | +Save the flow. The operation will use these options at runtime. |
| 50 | + |
| 51 | +## Trigger the flow via HTTP (Directus API) |
| 52 | + |
| 53 | +Manual trigger (flow id `569895a0-7a65-4cb2-94aa-67f77a776a08` in the sample exports): |
| 54 | + |
| 55 | +```bash |
| 56 | +CMS_API_URL="http://localhost:8055" |
| 57 | +CMS_API_TOKEN="<ADMIN_OR_ALLOWED_TOKEN>" |
| 58 | +FLOW_ID="569895a0-7a65-4cb2-94aa-67f77a776a08" |
| 59 | + |
| 60 | +curl -X POST \ |
| 61 | + "$CMS_API_URL/flows/trigger/$FLOW_ID" \ |
| 62 | + -H "Authorization: Bearer $CMS_API_TOKEN" \ |
| 63 | + -H "Content-Type: application/json" \ |
| 64 | + -d '{}' |
| 65 | +``` |
| 66 | + |
| 67 | +### Webhook trigger and inline payload (optional) |
| 68 | + |
| 69 | +This repo also includes a webhook flow (`9039bef8-fdc3-4f31-b5ab-7fee31273921`). If you POST a body containing a payload compatible with `src/schema/ApiPayloadSchema.ts`, the operation will validate and use this body instead of fetching from `url`. |
| 70 | + |
| 71 | +Example (send the whole api.json as request body): |
| 72 | + |
| 73 | +```bash |
| 74 | +CMS_API_URL="http://localhost:8055" |
| 75 | +CMS_API_TOKEN="<ADMIN_OR_ALLOWED_TOKEN>" |
| 76 | +FLOW_ID="9039bef8-fdc3-4f31-b5ab-7fee31273921" |
| 77 | + |
| 78 | +curl -X POST \ |
| 79 | + "$CMS_API_URL/flows/trigger/$FLOW_ID" \ |
| 80 | + -H "Authorization: Bearer $CMS_API_TOKEN" \ |
| 81 | + -H "Content-Type: application/json" \ |
| 82 | + --data-binary @api.json |
| 83 | +``` |
| 84 | + |
| 85 | +## Permissions required |
| 86 | + |
| 87 | +This operation runs with the flow runner’s role/policy (`accountability: "all"` in the exported flows). Ensure the role used to trigger the flow has at least: |
| 88 | + |
| 89 | +- `packages`: read, create, update (the operation may create missing packages) |
| 90 | +- `package_symbols`: read, create, update (the operation reads by `id`, then creates/updates symbols) |
| 91 | + |
| 92 | +If these permissions are missing, the flow will fail with `403 You don't have permission to access this.` during upsert. |
| 93 | + |
| 94 | +## Testing |
| 95 | + |
| 96 | +Run the unit tests for the mapper and the service: |
| 97 | + |
| 98 | +```bash |
| 99 | +yarn vitest run \ |
| 100 | + extensions/tsed-flow-package-symbols/src/mappers/mapApiSymbolToDirectus.spec.ts \ |
| 101 | + packages/usecases/package-symbols/PackageSymbolsService.spec.ts |
| 102 | +``` |
| 103 | + |
| 104 | +Both tests should pass. |
| 105 | + |
| 106 | +## Notes |
| 107 | + |
| 108 | +- The `ApiPayloadSchema` strictly types the `api.json` format used by the operation. |
| 109 | +- The mapper intentionally keeps logic minimal and assumes the contract is stable. |
0 commit comments