UNPKG

wgc

Version:

The official CLI tool to manage the GraphQL Federation Platform Cosmo

182 lines 7.28 kB
import { writeFile } from 'node:fs/promises'; export class JsonCheckSchemaOutputBuilder { constructor(code, rowLimit, outFile) { this.data = { status: 'error', code, rowLimit }; this.outFile = outFile; } setUrl(url) { this.data.url = url; return this; } setCode(code) { this.data.code = code; return this; } setStatus(success) { this.data.status = success ? 'success' : 'error'; return this; } setMessage(message) { this.data.message = message; return this; } setDetails(details) { this.data.details = details; return this; } setProposals(message) { this.data.proposals = { message }; return this; } initProposals(message) { var _a; var _b; (_a = (_b = this.data).proposals) !== null && _a !== void 0 ? _a : (_b.proposals = { message }); return this; } setTraffic(message) { this.data.traffic = { message }; return this; } setFeatureSubgraphCheck(message) { this.data.featureSubgraphCheck = { message }; return this; } markTrafficLinkedFailed(fallbackMessage) { var _a, _b; this.data.traffic = { message: (_b = (_a = this.data.traffic) === null || _a === void 0 ? void 0 : _a.message) !== null && _b !== void 0 ? _b : fallbackMessage, }; return this; } addBreakingChanges(changes) { var _a, _b, _c, _d; this.data.changes = { ...this.data.changes, breaking: [...((_b = (_a = this.data.changes) === null || _a === void 0 ? void 0 : _a.breaking) !== null && _b !== void 0 ? _b : []), ...changes], nonBreaking: [...((_d = (_c = this.data.changes) === null || _c === void 0 ? void 0 : _c.nonBreaking) !== null && _d !== void 0 ? _d : [])], }; return this; } addNonBreakingChanges(changes) { var _a, _b, _c, _d; this.data.changes = { breaking: [...((_b = (_a = this.data.changes) === null || _a === void 0 ? void 0 : _a.breaking) !== null && _b !== void 0 ? _b : [])], nonBreaking: [...((_d = (_c = this.data.changes) === null || _c === void 0 ? void 0 : _c.nonBreaking) !== null && _d !== void 0 ? _d : []), ...changes], }; return this; } setOperationUsageStats(stats) { var _a; var _b; (_a = (_b = this.data).operationUsageStats) !== null && _a !== void 0 ? _a : (_b.operationUsageStats = stats); return this; } addCompositionErrors(errors) { var _a, _b, _c, _d; this.data.composition = { errors: [...((_b = (_a = this.data.composition) === null || _a === void 0 ? void 0 : _a.errors) !== null && _b !== void 0 ? _b : []), ...errors], warnings: [...((_d = (_c = this.data.composition) === null || _c === void 0 ? void 0 : _c.warnings) !== null && _d !== void 0 ? _d : [])], }; return this; } addCompositionWarnings(warnings) { var _a, _b, _c, _d; this.data.composition = { errors: [...((_b = (_a = this.data.composition) === null || _a === void 0 ? void 0 : _a.errors) !== null && _b !== void 0 ? _b : [])], warnings: [...((_d = (_c = this.data.composition) === null || _c === void 0 ? void 0 : _c.warnings) !== null && _d !== void 0 ? _d : []), ...warnings], }; return this; } addLintErrors(errors) { var _a, _b, _c, _d; this.data.lint = { errors: [...((_b = (_a = this.data.lint) === null || _a === void 0 ? void 0 : _a.errors) !== null && _b !== void 0 ? _b : []), ...errors], warnings: [...((_d = (_c = this.data.lint) === null || _c === void 0 ? void 0 : _c.warnings) !== null && _d !== void 0 ? _d : [])], }; return this; } addLintWarnings(warnings) { var _a, _b, _c, _d; this.data.lint = { errors: [...((_b = (_a = this.data.lint) === null || _a === void 0 ? void 0 : _a.errors) !== null && _b !== void 0 ? _b : [])], warnings: [...((_d = (_c = this.data.lint) === null || _c === void 0 ? void 0 : _c.warnings) !== null && _d !== void 0 ? _d : []), ...warnings], }; return this; } addGraphPruneErrors(errors) { var _a, _b, _c, _d; this.data.graphPrune = { errors: [...((_b = (_a = this.data.graphPrune) === null || _a === void 0 ? void 0 : _a.errors) !== null && _b !== void 0 ? _b : []), ...errors], warnings: [...((_d = (_c = this.data.graphPrune) === null || _c === void 0 ? void 0 : _c.warnings) !== null && _d !== void 0 ? _d : [])], }; return this; } addGraphPruneWarnings(warnings) { var _a, _b, _c, _d; this.data.graphPrune = { errors: [...((_b = (_a = this.data.graphPrune) === null || _a === void 0 ? void 0 : _a.errors) !== null && _b !== void 0 ? _b : [])], warnings: [...((_d = (_c = this.data.graphPrune) === null || _c === void 0 ? void 0 : _c.warnings) !== null && _d !== void 0 ? _d : []), ...warnings], }; return this; } markGraphPruneLinkedFailed() { var _a, _b, _c, _d; this.data.graphPrune = { errors: [...((_b = (_a = this.data.graphPrune) === null || _a === void 0 ? void 0 : _a.errors) !== null && _b !== void 0 ? _b : [])], warnings: [...((_d = (_c = this.data.graphPrune) === null || _c === void 0 ? void 0 : _c.warnings) !== null && _d !== void 0 ? _d : [])], }; return this; } addComposedSchemaBreakingChanges(changes) { var _a; this.data.composedSchemaBreakingChanges = [...((_a = this.data.composedSchemaBreakingChanges) !== null && _a !== void 0 ? _a : []), ...changes]; return this; } setExtensionError(message) { this.data.extensions = { message }; return this; } setExceededRowLimit(exceeded) { this.data.exceededRowLimit = exceeded; return this; } build() { return this.data; } /** * Writes valid JSON either to stdout or a file. */ async write() { if (this.outFile) { await writeFile(this.outFile, this.serializeOutput(true)); } else { console.log(this.serializeOutput()); } } /** * JSON.stringify replacer that strips protobuf V2 internal fields * (like $typeName) so the output remains a clean, user-facing format. */ static jsonReplacer(_key, value) { if (value != null && typeof value === 'object' && !Array.isArray(value)) { const record = value; if ('$typeName' in record) { const { $typeName, ...rest } = record; return rest; } } return value; } serializeOutput(formatted = false) { try { return JSON.stringify(this.build(), JsonCheckSchemaOutputBuilder.jsonReplacer, formatted ? 2 : 0); } catch (err) { console.error('Failed to serialize JSON data.'); throw err; } } } //# sourceMappingURL=json-check-schema-output-builder.js.map