import { ElementSchema, ElementTypeDefinition, GetAttributesResponse, OrganizationSettings, SchemaHistoryEntry, SchemaHistorySummary, SchemaId, Scope, ScopeResponse, Stage, Template, TemplateHistoryEntry, TemplateHistorySummary, TemplateId, } from '../../interfaces' import { Api } from '../api' export const EDITORS_API = '/service/editors/new' export const DEFAULT_TEMPLATE_ID = 'Default' type ShortStage = 'prod' | 'stage' export const SHORT_STAGE_MAP: Record = { production: 'prod', stage: 'stage', } export class Editors { api: Api constructor(api: Api) { this.api = api } // V1 getLegacyTypeDefinition(type: string) { return this.api.apisauce.get( `${EDITORS_API}/v1/elementtypes/${type}`, ) } // V2 addSchema( data: Required>, options?: { stage?: Stage }, ) { return this.api.apisauce.post( `${EDITORS_API}/v2/schema`, data, { params: options?.stage ? { stage: SHORT_STAGE_MAP[options.stage] } : undefined, }, ) } getScope(scope?: Scope, options?: { stage?: Stage }) { return this.api.apisauce.get( `${EDITORS_API}/v2/schema${scope ? `/${scope}` : ''}`, options?.stage ? { stage: SHORT_STAGE_MAP[options.stage] } : undefined, ) } getAttributeSummary(scope?: Scope, options?: { stage?: Stage }) { return this.api.apisauce.get( `${EDITORS_API}/v2/attributes${scope ? `/${scope}` : ''}`, options?.stage ? { stage: SHORT_STAGE_MAP[options.stage] } : undefined, ) } getScopeList(options?: { stage?: Stage }) { return this.api.apisauce.get<{ id: string; name: string }[], void>( `${EDITORS_API}/v2/scopes`, options?.stage ? { stage: SHORT_STAGE_MAP[options.stage] } : undefined, ) } getTypeSchema( schemaId: SchemaId, options?: { stage?: Stage; cache?: false }, ) { return this.api.apisauce.get( `${EDITORS_API}/v2/schema/${schemaId}`, options?.stage ? { stage: SHORT_STAGE_MAP[options.stage] } : undefined, options?.cache !== undefined ? { cache: options?.cache, } : undefined, ) } saveTypeSchema( schemaId: SchemaId, data: Partial, options?: { stage?: Stage }, ) { return this.api.apisauce.post< ElementSchema & { children?: ElementSchema[] }, void >(`${EDITORS_API}/v2/schema/${schemaId}`, data, { params: options?.stage ? { stage: SHORT_STAGE_MAP[options.stage] } : undefined, }) } getTemplate( schemaId: SchemaId, templateId = DEFAULT_TEMPLATE_ID, options?: { stage?: Stage; cache?: false; strict?: boolean }, ) { const params: Record = {} if (options?.strict) { params.strict = 'true' } if (options?.stage) { params.stage = SHORT_STAGE_MAP[options.stage] } return this.api.apisauce.get( `${EDITORS_API}/v2/template/${schemaId}/${templateId}`, params, options?.cache !== undefined ? { cache: options?.cache, } : undefined, ) } saveTemplate( schemaId: SchemaId, templateId?: TemplateId, data?: Partial