UNPKG

@creamapi/cream

Version:

Concise REST API Maker - An extension library for express to create REST APIs faster

52 lines (51 loc) 2.02 kB
"use strict"; /* * Copyright 2024 Raul Radu * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.SerializerMetaInfo = exports.SERIALIZER_META_INFO_METADATA_KEY = void 0; exports.Meta = Meta; exports.SERIALIZER_META_INFO_METADATA_KEY = Symbol('cream:data-serializer'); /** * @internal * This class is used to store meta information for * the serializers that will be used to make decisions * when serializing objects */ class SerializerMetaInfo { attributes = []; addAttribute(attribute) { this.attributes.push(attribute); } hasAttribute(attribute) { return this.attributes.indexOf(attribute) != -1; } } exports.SerializerMetaInfo = SerializerMetaInfo; /** * Defines a metadata for the attribute that can be used by * serializers to make decisions on serialization. * @example the XML serializer uses the attribute AutoIndex to automatically * add the index of the element as the attribute index in the xml tag * @param attribute the attribute that should be defined * @returns the decorator that will decorate the attribute */ function Meta(attribute) { return function (target, propertyName) { let metaInfo = Reflect.getMetadata(exports.SERIALIZER_META_INFO_METADATA_KEY, target, propertyName) || new SerializerMetaInfo(); metaInfo.addAttribute(attribute); Reflect.defineMetadata(exports.SERIALIZER_META_INFO_METADATA_KEY, metaInfo, target, propertyName); }; }