|
| 1 | +import type { Schema as ISchema } from "@smithy/types"; |
| 2 | + |
| 3 | +import { ErrorSchema } from "./schemas/ErrorSchema"; |
| 4 | + |
| 5 | +/** |
| 6 | + * A way to look up schema by their ShapeId values. |
| 7 | + * |
| 8 | + * @alpha |
| 9 | + */ |
| 10 | +export class TypeRegistry { |
| 11 | + public static readonly registries = new Map<string, TypeRegistry>(); |
| 12 | + |
| 13 | + private constructor( |
| 14 | + public readonly namespace: string, |
| 15 | + private schemas: Map<string, ISchema> = new Map() |
| 16 | + ) {} |
| 17 | + |
| 18 | + /** |
| 19 | + * @param namespace - specifier. |
| 20 | + * @returns the schema for that namespace, creating it if necessary. |
| 21 | + */ |
| 22 | + public static for(namespace: string): TypeRegistry { |
| 23 | + if (!TypeRegistry.registries.has(namespace)) { |
| 24 | + TypeRegistry.registries.set(namespace, new TypeRegistry(namespace)); |
| 25 | + } |
| 26 | + return TypeRegistry.registries.get(namespace)!; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Adds the given schema to a type registry with the same namespace. |
| 31 | + * |
| 32 | + * @param shapeId - to be registered. |
| 33 | + * @param schema - to be registered. |
| 34 | + */ |
| 35 | + public register(shapeId: string, schema: ISchema) { |
| 36 | + const qualifiedName = this.normalizeShapeId(shapeId); |
| 37 | + const registry = TypeRegistry.for(this.getNamespace(shapeId)); |
| 38 | + registry.schemas.set(qualifiedName, schema); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @param shapeId - query. |
| 43 | + * @returns the schema. |
| 44 | + */ |
| 45 | + public getSchema(shapeId: string): ISchema { |
| 46 | + const id = this.normalizeShapeId(shapeId); |
| 47 | + if (!this.schemas.has(id)) { |
| 48 | + throw new Error(`@smithy/core/schema - schema not found for ${id}`); |
| 49 | + } |
| 50 | + return this.schemas.get(id)!; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * The smithy-typescript code generator generates a synthetic (i.e. unmodeled) base exception, |
| 55 | + * because generated SDKs before the introduction of schemas have the notion of a ServiceBaseException, which |
| 56 | + * is unique per service/model. |
| 57 | + * |
| 58 | + * This is generated under a unique prefix that is combined with the service namespace, and this |
| 59 | + * method is used to retrieve it. |
| 60 | + * |
| 61 | + * The base exception synthetic schema is used when an error is returned by a service, but we cannot |
| 62 | + * determine what existing schema to use to deserialize it. |
| 63 | + * |
| 64 | + * @returns the synthetic base exception of the service namespace associated with this registry instance. |
| 65 | + */ |
| 66 | + public getBaseException(): ErrorSchema | undefined { |
| 67 | + for (const [id, schema] of this.schemas.entries()) { |
| 68 | + if (id.startsWith("smithyts.client.synthetic.") && id.endsWith("ServiceException")) { |
| 69 | + return schema as ErrorSchema; |
| 70 | + } |
| 71 | + } |
| 72 | + return undefined; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param predicate - criterion. |
| 77 | + * @returns a schema in this registry matching the predicate. |
| 78 | + */ |
| 79 | + public find(predicate: (schema: ISchema) => boolean) { |
| 80 | + return [...this.schemas.values()].find(predicate); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Unloads the current TypeRegistry. |
| 85 | + */ |
| 86 | + public destroy() { |
| 87 | + TypeRegistry.registries.delete(this.namespace); |
| 88 | + this.schemas.clear(); |
| 89 | + } |
| 90 | + |
| 91 | + private normalizeShapeId(shapeId: string) { |
| 92 | + if (shapeId.includes("#")) { |
| 93 | + return shapeId; |
| 94 | + } |
| 95 | + return this.namespace + "#" + shapeId; |
| 96 | + } |
| 97 | + |
| 98 | + private getNamespace(shapeId: string) { |
| 99 | + return this.normalizeShapeId(shapeId).split("#")[0]; |
| 100 | + } |
| 101 | +} |
0 commit comments