UNPKG

@n0safe/indirectus

Version:
143 lines 5.77 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchSchema = fetchSchema; const fs = __importStar(require("node:fs")); const sdk_1 = require("@directus/sdk"); const node_path_1 = __importDefault(require("node:path")); const ignoredCollections = [ "directus_migrations", "directus_sessions", "directus_access", "directus_comments", ]; async function fetchSchema(directus, options = { useCache: false, }) { const cache = options?.cache ?? process.env.DIRECTUS_SCHEMA_CACHE; const useCache = options?.useCache ?? (process.env.DIRECTUS_SCHEMA_CACHE && true); let raw = null; if (cache) { const cacheDir = node_path_1.default.dirname(cache); if (!fs.existsSync(cacheDir)) { fs.mkdirSync(cacheDir, { recursive: true }); } if (fs.existsSync(cache)) { if (!fs.statSync(cache).isFile()) { throw new Error("Cache path is not a file"); } if (useCache) { try { raw = JSON.parse(fs.readFileSync(cache, "utf-8")); } catch (e) { raw = null; } } } } if (!raw) { const client = (0, sdk_1.createDirectus)(directus.url) .with((0, sdk_1.rest)()) .with((0, sdk_1.staticToken)(directus.token)); try { const pong = await client.request((0, sdk_1.serverPing)()); if (pong != "pong") { throw new Error(`Server did not respond with 'pong'.\nPerhaps URL is invalid: ${directus.url}\n\nATTENTION:Note that the URL must point to Directus root (for example, do not include /admin)`); } } catch (e) { throw new Error(`Failed to ping Directus server at ${directus.url}: ${e.message}`); } const collections = await client.request((0, sdk_1.readCollections)()); const fields = await client.request((0, sdk_1.readFields)()); const relations = await client.request((0, sdk_1.readRelations)()); // Patch for https://github.com/directus/directus/issues/20475 const favicon = relations.find((r) => r.collection == "directus_settings" && r.field == "public_favicon"); if (favicon && favicon.meta == null) { favicon.meta = { system: true, many_collection: "directus_settings", many_field: "public_favicon", one_collection: "directus_files", one_field: null, one_allowed_collections: null, one_collection_field: null, one_deselect_action: "nullify", junction_field: null, sort_field: null, }; } const url = new URL(directus.url); url.pathname = (url.pathname?.replace(/\/$/, "") ?? "") + "/schema/snapshot"; url.searchParams.set("export", "json"); url.searchParams.set("access_token", directus.token); const response = await fetch(url); if (!response.ok) { throw new Error(`Failed to fetch schema (returned status ${response.status}): ${response.statusText}\n${await response.text()}`); } let body = ""; let schema = undefined; try { body = await response.text(); schema = JSON.parse(body); } catch (e) { throw new Error(`Server did not return JSON on schema export (${response.headers.get("content-type") ?? "<missing content type>"}): ${body}`); } raw = { // TODO: needs deep merging or fill the meta object ...schema, fields, collections, relations, }; } if (cache) { fs.writeFileSync(cache, JSON.stringify(raw, null, 2)); } const result = { ...raw, fields: raw.fields.filter((field) => ignoredCollections.indexOf(field.collection) < 0), collections: raw.collections.filter((col) => ignoredCollections.indexOf(col.collection) < 0 && col.schema !== null), relations: raw.relations, }; return result; } //# sourceMappingURL=schema.js.map