@bosonprotocol/metadata
Advanced tools
Comparing version 1.5.0 to 1.6.0-alpha.0
import { SchemaOf } from "yup"; | ||
export declare const productV1MetadataSchema: SchemaOf<ProductV1Metadata>; | ||
declare type ProductBase = { | ||
export declare type ProductBase = { | ||
title: string; | ||
@@ -26,5 +26,5 @@ description: string; | ||
packaging_dimensions_height?: string; | ||
packaging_dimensions_unit: string; | ||
packaging_weight_value: string; | ||
packaging_weight_unit: string; | ||
packaging_dimensions_unit?: string; | ||
packaging_weight_value?: string; | ||
packaging_weight_unit?: string; | ||
}; | ||
@@ -91,3 +91,3 @@ declare type ProductDetails = { | ||
}[]; | ||
product: ProductBase | ProductDetails | { | ||
product: ProductBase & ProductDetails & { | ||
uuid: string; | ||
@@ -100,5 +100,11 @@ version: number; | ||
exchangePolicy: ExchangePolicy; | ||
productOverrides?: ProductBase; | ||
productOverrides?: Partial<ProductBase>; | ||
}; | ||
export declare type ProductV1Variant = Array<Variation>; | ||
export declare function createVariantProductMetadata(productMetadata: ProductV1Metadata, variants: Array<{ | ||
productVariant: ProductV1Variant; | ||
productOverrides?: Partial<ProductBase>; | ||
}>): Array<ProductV1Metadata>; | ||
export declare function buildUuid(): string; | ||
export {}; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -6,6 +6,75 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.productV1MetadataSchema = void 0; | ||
exports.buildUuid = exports.createVariantProductMetadata = exports.productV1MetadataSchema = void 0; | ||
const schema_to_yup_1 = require("schema-to-yup"); | ||
const schema_json_1 = __importDefault(require("./schema.json")); | ||
exports.productV1MetadataSchema = (0, schema_to_yup_1.buildYup)(schema_json_1.default, {}); | ||
function createVariantProductMetadata(productMetadata, variants) { | ||
// Build the metadata without the overrides | ||
const metadatas = buildVariantProductMetadata(productMetadata, variants.map((variant) => variant.productVariant)); | ||
// Apply the overrides when present | ||
for (let index = 0; index < metadatas.length; index++) { | ||
metadatas[index].productOverrides = | ||
variants[index].productOverrides || undefined; | ||
// In case the productOverrides is 'null', assign 'undefined' | ||
} | ||
return metadatas; | ||
} | ||
exports.createVariantProductMetadata = createVariantProductMetadata; | ||
function buildVariantProductMetadata(productMetadata, variants) { | ||
// Check the productMetadata does not have any variations | ||
if (productMetadata.variations) { | ||
throw new Error("Unable to create variant product Metadata from an already existing variation"); | ||
} | ||
if (variants.length < 2) { | ||
throw new Error("Unable to create a variant product with less than 2 variants"); | ||
} | ||
// Check the array of variants is consistent (each variant would have the same types of variations and different values) | ||
const [variant0, ...nextVariants] = variants; | ||
const types0 = variant0.map((variation) => variation.type); | ||
const variantsStringMap = new Map(); | ||
variantsStringMap.set(serializeVariant(variant0), serializeVariant(variant0)); | ||
for (const variant of nextVariants) { | ||
const variantStr = serializeVariant(variant); | ||
if (variantsStringMap.has(variantStr)) { | ||
throw new Error(`Redundant variant ${variantStr}`); | ||
} | ||
variantsStringMap.set(variantStr, variantStr); | ||
if (variant.length !== types0.length) { | ||
throw new Error("variants are not consistent to each other"); | ||
} | ||
types0.forEach((type) => { | ||
const variation = variant.find((v) => v.type === type); | ||
if (!variation) { | ||
throw new Error(`missing type ${type} in variant ${serializeVariant(variant)}`); | ||
} | ||
}); | ||
} | ||
// Each variant should have an different UUID | ||
return variants.map((variant) => { | ||
return Object.assign(Object.assign({}, productMetadata), { uuid: buildUuid(), variations: variant }); | ||
}); | ||
} | ||
function buildUuid() { | ||
if (typeof window !== "undefined" && (window === null || window === void 0 ? void 0 : window.crypto)) { | ||
return window.crypto.randomUUID(); | ||
} | ||
else { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const crypto = require("crypto"); | ||
return crypto.randomUUID(); | ||
} | ||
} | ||
exports.buildUuid = buildUuid; | ||
function serializeVariant(variant) { | ||
// Be sure each variation structure has its keys ordered | ||
const orderedStruct = variant.map((variation) => Object.keys(variation) | ||
.sort() | ||
.reduce((obj, key) => { | ||
obj[key] = variation[key]; | ||
return obj; | ||
}, {})); | ||
// Be sure each variation in the table is ordered per type | ||
const orderedTable = orderedStruct.sort((a, b) => a.type.localeCompare(b.type)); | ||
return JSON.stringify(orderedTable); | ||
} | ||
//# sourceMappingURL=index.js.map |
import { SchemaOf } from "yup"; | ||
export declare const productV1MetadataSchema: SchemaOf<ProductV1Metadata>; | ||
declare type ProductBase = { | ||
export declare type ProductBase = { | ||
title: string; | ||
@@ -26,5 +26,5 @@ description: string; | ||
packaging_dimensions_height?: string; | ||
packaging_dimensions_unit: string; | ||
packaging_weight_value: string; | ||
packaging_weight_unit: string; | ||
packaging_dimensions_unit?: string; | ||
packaging_weight_value?: string; | ||
packaging_weight_unit?: string; | ||
}; | ||
@@ -91,3 +91,3 @@ declare type ProductDetails = { | ||
}[]; | ||
product: ProductBase | ProductDetails | { | ||
product: ProductBase & ProductDetails & { | ||
uuid: string; | ||
@@ -100,5 +100,11 @@ version: number; | ||
exchangePolicy: ExchangePolicy; | ||
productOverrides?: ProductBase; | ||
productOverrides?: Partial<ProductBase>; | ||
}; | ||
export declare type ProductV1Variant = Array<Variation>; | ||
export declare function createVariantProductMetadata(productMetadata: ProductV1Metadata, variants: Array<{ | ||
productVariant: ProductV1Variant; | ||
productOverrides?: Partial<ProductBase>; | ||
}>): Array<ProductV1Metadata>; | ||
export declare function buildUuid(): string; | ||
export {}; | ||
//# sourceMappingURL=index.d.ts.map |
import { buildYup } from "schema-to-yup"; | ||
import schema from "./schema.json"; | ||
export const productV1MetadataSchema = buildYup(schema, {}); | ||
export function createVariantProductMetadata(productMetadata, variants) { | ||
// Build the metadata without the overrides | ||
const metadatas = buildVariantProductMetadata(productMetadata, variants.map((variant) => variant.productVariant)); | ||
// Apply the overrides when present | ||
for (let index = 0; index < metadatas.length; index++) { | ||
metadatas[index].productOverrides = | ||
variants[index].productOverrides || undefined; | ||
// In case the productOverrides is 'null', assign 'undefined' | ||
} | ||
return metadatas; | ||
} | ||
function buildVariantProductMetadata(productMetadata, variants) { | ||
// Check the productMetadata does not have any variations | ||
if (productMetadata.variations) { | ||
throw new Error("Unable to create variant product Metadata from an already existing variation"); | ||
} | ||
if (variants.length < 2) { | ||
throw new Error("Unable to create a variant product with less than 2 variants"); | ||
} | ||
// Check the array of variants is consistent (each variant would have the same types of variations and different values) | ||
const [variant0, ...nextVariants] = variants; | ||
const types0 = variant0.map((variation) => variation.type); | ||
const variantsStringMap = new Map(); | ||
variantsStringMap.set(serializeVariant(variant0), serializeVariant(variant0)); | ||
for (const variant of nextVariants) { | ||
const variantStr = serializeVariant(variant); | ||
if (variantsStringMap.has(variantStr)) { | ||
throw new Error(`Redundant variant ${variantStr}`); | ||
} | ||
variantsStringMap.set(variantStr, variantStr); | ||
if (variant.length !== types0.length) { | ||
throw new Error("variants are not consistent to each other"); | ||
} | ||
types0.forEach((type) => { | ||
const variation = variant.find((v) => v.type === type); | ||
if (!variation) { | ||
throw new Error(`missing type ${type} in variant ${serializeVariant(variant)}`); | ||
} | ||
}); | ||
} | ||
// Each variant should have an different UUID | ||
return variants.map((variant) => { | ||
return { | ||
...productMetadata, | ||
uuid: buildUuid(), | ||
variations: variant | ||
}; | ||
}); | ||
} | ||
export function buildUuid() { | ||
if (typeof window !== "undefined" && window?.crypto) { | ||
return window.crypto.randomUUID(); | ||
} | ||
else { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const crypto = require("crypto"); | ||
return crypto.randomUUID(); | ||
} | ||
} | ||
function serializeVariant(variant) { | ||
// Be sure each variation structure has its keys ordered | ||
const orderedStruct = variant.map((variation) => Object.keys(variation) | ||
.sort() | ||
.reduce((obj, key) => { | ||
obj[key] = variation[key]; | ||
return obj; | ||
}, {})); | ||
// Be sure each variation in the table is ordered per type | ||
const orderedTable = orderedStruct.sort((a, b) => a.type.localeCompare(b.type)); | ||
return JSON.stringify(orderedTable); | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@bosonprotocol/metadata", | ||
"version": "1.5.0", | ||
"version": "1.6.0-alpha.0", | ||
"description": "Offer metadata schemas, types and tools of the Boson Protocol.", | ||
@@ -42,3 +42,3 @@ "main": "./dist/cjs/index.js", | ||
}, | ||
"gitHead": "9670a17ce667a6622ab07e77d7750ea446370f7c" | ||
"gitHead": "e8bbe19657acf93c845736d68876ea6f29279547" | ||
} |
@@ -10,3 +10,3 @@ import { buildYup } from "schema-to-yup"; | ||
type ProductBase = { | ||
export type ProductBase = { | ||
title: string; | ||
@@ -34,5 +34,5 @@ description: string; | ||
packaging_dimensions_height?: string; | ||
packaging_dimensions_unit: string; | ||
packaging_weight_value: string; | ||
packaging_weight_unit: string; | ||
packaging_dimensions_unit?: string; | ||
packaging_weight_value?: string; | ||
packaging_weight_unit?: string; | ||
}; | ||
@@ -105,9 +105,7 @@ | ||
}[]; | ||
product: | ||
| ProductBase | ||
| ProductDetails | ||
| { | ||
uuid: string; | ||
version: number; | ||
}; | ||
product: ProductBase & | ||
ProductDetails & { | ||
uuid: string; | ||
version: number; | ||
}; | ||
variations?: Variation[]; | ||
@@ -117,3 +115,104 @@ seller: SellerMetadata; | ||
exchangePolicy: ExchangePolicy; | ||
productOverrides?: ProductBase; | ||
productOverrides?: Partial<ProductBase>; | ||
}; | ||
export type ProductV1Variant = Array<Variation>; | ||
export function createVariantProductMetadata( | ||
productMetadata: ProductV1Metadata, | ||
variants: Array<{ | ||
productVariant: ProductV1Variant; | ||
productOverrides?: Partial<ProductBase>; | ||
}> | ||
): Array<ProductV1Metadata> { | ||
// Build the metadata without the overrides | ||
const metadatas = buildVariantProductMetadata( | ||
productMetadata, | ||
variants.map((variant) => variant.productVariant) | ||
); | ||
// Apply the overrides when present | ||
for (let index = 0; index < metadatas.length; index++) { | ||
metadatas[index].productOverrides = | ||
variants[index].productOverrides || undefined; | ||
// In case the productOverrides is 'null', assign 'undefined' | ||
} | ||
return metadatas; | ||
} | ||
function buildVariantProductMetadata( | ||
productMetadata: ProductV1Metadata, | ||
variants: Array<ProductV1Variant> | ||
): Array<ProductV1Metadata> { | ||
// Check the productMetadata does not have any variations | ||
if (productMetadata.variations) { | ||
throw new Error( | ||
"Unable to create variant product Metadata from an already existing variation" | ||
); | ||
} | ||
if (variants.length < 2) { | ||
throw new Error( | ||
"Unable to create a variant product with less than 2 variants" | ||
); | ||
} | ||
// Check the array of variants is consistent (each variant would have the same types of variations and different values) | ||
const [variant0, ...nextVariants] = variants; | ||
const types0 = variant0.map((variation) => variation.type); | ||
const variantsStringMap = new Map<string, string>(); | ||
variantsStringMap.set(serializeVariant(variant0), serializeVariant(variant0)); | ||
for (const variant of nextVariants) { | ||
const variantStr = serializeVariant(variant); | ||
if (variantsStringMap.has(variantStr)) { | ||
throw new Error(`Redundant variant ${variantStr}`); | ||
} | ||
variantsStringMap.set(variantStr, variantStr); | ||
if (variant.length !== types0.length) { | ||
throw new Error("variants are not consistent to each other"); | ||
} | ||
types0.forEach((type) => { | ||
const variation = variant.find((v) => v.type === type); | ||
if (!variation) { | ||
throw new Error( | ||
`missing type ${type} in variant ${serializeVariant(variant)}` | ||
); | ||
} | ||
}); | ||
} | ||
// Each variant should have an different UUID | ||
return variants.map((variant) => { | ||
return { | ||
...productMetadata, | ||
uuid: buildUuid(), | ||
variations: variant | ||
}; | ||
}); | ||
} | ||
export function buildUuid(): string { | ||
if (typeof window !== "undefined" && window?.crypto) { | ||
return window.crypto.randomUUID(); | ||
} else { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const crypto = require("crypto"); | ||
return crypto.randomUUID(); | ||
} | ||
} | ||
function serializeVariant(variant: ProductV1Variant): string { | ||
// Be sure each variation structure has its keys ordered | ||
const orderedStruct = variant.map((variation) => | ||
Object.keys(variation) | ||
.sort() | ||
.reduce((obj, key) => { | ||
obj[key] = variation[key]; | ||
return obj; | ||
}, {}) | ||
) as ProductV1Variant; | ||
// Be sure each variation in the table is ordered per type | ||
const orderedTable = orderedStruct.sort((a, b) => | ||
a.type.localeCompare(b.type) | ||
); | ||
return JSON.stringify(orderedTable); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
80927
1472
1