|
1 |
| -import { ISchema, ISchemaTemplateSchema, ISchemaTemplate} from './ISchema'; |
2 |
| -import { v4 as uuidv4 } from 'uuid'; |
3 |
| -import * as constant from '../constants' |
4 |
| -import Utils from '../utils'; |
| 1 | +import { ISchema, ISchemaTemplateSchema, ISchemaTemplate } from "./ISchema"; |
| 2 | +import { v4 as uuidv4 } from "uuid"; |
| 3 | +import * as constant from "../constants"; |
| 4 | +import Utils from "../utils"; |
5 | 5 | import axios from "axios";
|
6 |
| -import IOptions from '../IOptions'; |
| 6 | +import IOptions from "../IOptions"; |
7 | 7 |
|
8 | 8 | const SC_PREFIX = "sch_";
|
9 | 9 | const SCHEMA_URL = "https://json-schema.org/draft-07/schema#";
|
10 |
| -const W3_SCHEMA_JSON_URL = "https://w3c-ccg.github.io/vc-json-schemas/schema/1.0/schema.json"; |
| 10 | +const W3_SCHEMA_JSON_URL = |
| 11 | + "https://w3c-ccg.github.io/vc-json-schemas/schema/1.0/schema.json"; |
11 | 12 |
|
12 |
| -class SchemaTemplateSchema implements ISchemaTemplateSchema{ |
13 |
| - $schema: string; |
14 |
| - description: string; |
15 |
| - type: string; |
16 |
| - properties: any; |
17 |
| - required: Array<string>; |
18 |
| - additionalProperties: boolean |
19 |
| - constructor({properties, additionalProperties, description}: ISchemaTemplateSchema){ |
20 |
| - this.$schema = SCHEMA_URL; |
21 |
| - this.description = description; |
22 |
| - this.type = "object"; |
23 |
| - this.properties = {}; |
24 |
| - this.required = Object.keys(properties); // TODO: right now all requried... later we can change this. |
25 |
| - this.additionalProperties = additionalProperties as boolean; |
26 |
| - this.required.forEach(key => { |
27 |
| - this.properties[`${key}`] = { |
28 |
| - type: typeof properties[key] ? typeof properties[key] : "string" |
29 |
| - } |
30 |
| - }); |
31 |
| - } |
| 13 | +class SchemaTemplateSchema implements ISchemaTemplateSchema { |
| 14 | + $schema: string; |
| 15 | + description: string; |
| 16 | + type: string; |
| 17 | + properties: any; |
| 18 | + required: Array<string>; |
| 19 | + additionalProperties: boolean; |
| 20 | + constructor({ |
| 21 | + properties, |
| 22 | + additionalProperties, |
| 23 | + description, |
| 24 | + }: ISchemaTemplateSchema) { |
| 25 | + this.$schema = SCHEMA_URL; |
| 26 | + this.description = description; |
| 27 | + this.type = "object"; |
| 28 | + this.properties = {}; |
| 29 | + this.required = Object.keys(properties); // TODO: right now all requried... later we can change this. |
| 30 | + this.additionalProperties = additionalProperties as boolean; |
| 31 | + this.required.forEach((key) => { |
| 32 | + this.properties[`${key}`] = { |
| 33 | + type: typeof properties[key] ? typeof properties[key] : "string", |
| 34 | + }; |
| 35 | + }); |
| 36 | + } |
32 | 37 |
|
33 |
| - get(): ISchemaTemplateSchema{ |
34 |
| - return this; |
35 |
| - } |
| 38 | + get(): ISchemaTemplateSchema { |
| 39 | + return this; |
| 40 | + } |
36 | 41 | }
|
37 | 42 |
|
38 |
| -export interface IScheme{ |
39 |
| - schemaUrl: string; |
40 |
| - generateSchema({ name, author, description, properties }: ISchema): Promise<ISchemaTemplate>; |
41 |
| - registerSchema(schema: ISchemaTemplate): Promise<any>; |
42 |
| - getSchema(schemaId: string): Promise<any>; |
| 43 | +export interface IScheme { |
| 44 | + schemaUrl: string; |
| 45 | + generateSchema({ |
| 46 | + name, |
| 47 | + author, |
| 48 | + description, |
| 49 | + properties, |
| 50 | + }: ISchema): Promise<ISchemaTemplate>; |
| 51 | + registerSchema(schema: ISchemaTemplate): Promise<any>; |
| 52 | + getSchema(options: {schemaId?: string, author?: string}): Promise<any>; |
43 | 53 | }
|
44 | 54 |
|
45 |
| -export default class Schema implements IScheme{ |
46 |
| - private utils: Utils; |
47 |
| - schemaUrl: string; |
48 |
| - constructor(options: IOptions) { |
49 |
| - this.utils = new Utils({ nodeUrl: options.nodeUrl }); |
50 |
| - this.schemaUrl = this.utils.nodeurl + constant.HYPERSIGN_NETWORK_SCHEMA_EP; |
51 |
| - } |
| 55 | +export default class Schema implements IScheme { |
| 56 | + private utils: Utils; |
| 57 | + schemaUrl: string; |
| 58 | + constructor(options: IOptions) { |
| 59 | + this.utils = new Utils({ nodeUrl: options.nodeUrl }); |
| 60 | + this.schemaUrl = this.utils.nodeurl + constant.HYPERSIGN_NETWORK_SCHEMA_EP; |
| 61 | + } |
52 | 62 |
|
53 |
| - public async generateSchema({ name, author, description, properties }: ISchema): Promise<ISchemaTemplate>{ |
54 |
| - let newSchema: ISchemaTemplate = {} as ISchemaTemplate; |
| 63 | + public async generateSchema({ |
| 64 | + name, |
| 65 | + author, |
| 66 | + description, |
| 67 | + properties, |
| 68 | + }: ISchema): Promise<ISchemaTemplate> { |
| 69 | + let newSchema: ISchemaTemplate = {} as ISchemaTemplate; |
55 | 70 |
|
56 |
| - const didDoc = await this.utils.resolve(author); |
57 |
| - if(!didDoc) throw new Error("Could not resolve author did"); |
| 71 | + const didDoc = await this.utils.resolve(author); |
| 72 | + if (!didDoc) throw new Error("Could not resolve author did"); |
58 | 73 |
|
59 |
| - newSchema.type = W3_SCHEMA_JSON_URL; |
60 |
| - newSchema.modelVersion = "v1.0"; |
61 |
| - newSchema.id = SC_PREFIX + uuidv4();; |
62 |
| - newSchema.name = name; |
63 |
| - newSchema.author = author; |
64 |
| - newSchema.authored = new Date().toString(); |
65 |
| - newSchema.schema = (new SchemaTemplateSchema({properties, additionalProperties: false , description})).get(); |
66 |
| - return newSchema; |
67 |
| - } |
| 74 | + newSchema.type = W3_SCHEMA_JSON_URL; |
| 75 | + newSchema.modelVersion = "v1.0"; |
| 76 | + newSchema.id = SC_PREFIX + uuidv4(); |
| 77 | + newSchema.name = name; |
| 78 | + newSchema.author = author; |
| 79 | + newSchema.authored = new Date().toString(); |
| 80 | + newSchema.schema = new SchemaTemplateSchema({ |
| 81 | + properties, |
| 82 | + additionalProperties: false, |
| 83 | + description, |
| 84 | + }).get(); |
| 85 | + return newSchema; |
| 86 | + } |
68 | 87 |
|
69 |
| - public async registerSchema(schema: ISchemaTemplate): Promise<any>{ |
70 |
| - try{ |
71 |
| - const response = await axios.post(this.schemaUrl, schema); |
72 |
| - return response.data; |
73 |
| - }catch(e){ |
74 |
| - const { response } = e; |
75 |
| - return response.data; |
76 |
| - } |
77 |
| - |
| 88 | + public async registerSchema(schema: ISchemaTemplate): Promise<any> { |
| 89 | + try { |
| 90 | + const response = await axios.post(this.schemaUrl, schema); |
| 91 | + return response.data; |
| 92 | + } catch (e) { |
| 93 | + const { response } = e; |
| 94 | + return response.data; |
78 | 95 | }
|
| 96 | + } |
79 | 97 |
|
80 |
| - public async getSchema(schemaId: string): Promise<any>{ |
81 |
| - try{ |
82 |
| - const get_didUrl = this.schemaUrl + schemaId; |
83 |
| - const response = await axios.get(get_didUrl); |
84 |
| - return response.data; |
85 |
| - }catch(e){ |
86 |
| - const { response } = e; |
87 |
| - return response.data; |
88 |
| - } |
89 |
| - |
90 |
| - } |
91 |
| - |
92 |
| -} |
| 98 | + public async getSchema(options: {schemaId?: string, author?: string}): Promise<any> { |
| 99 | + try { |
| 100 | + let get_didUrl = ""; |
| 101 | + const { author, schemaId } = options; |
93 | 102 |
|
| 103 | + if(author != undefined && schemaId != undefined){ // 1,1 |
| 104 | + get_didUrl = this.schemaUrl + schemaId + "?author=" + author; |
| 105 | + } |
94 | 106 |
|
| 107 | + else if(author == undefined && schemaId != undefined){ // 1,0 |
| 108 | + get_didUrl = this.schemaUrl + schemaId; |
| 109 | + } |
95 | 110 |
|
| 111 | + else if(author != undefined && schemaId == undefined){ // 0,1 |
| 112 | + get_didUrl = this.schemaUrl + "?author=" + author; |
| 113 | + } |
96 | 114 |
|
| 115 | + else if(author == undefined && schemaId == undefined){ // 0,0 |
| 116 | + get_didUrl = this.schemaUrl; |
| 117 | + } |
| 118 | + |
| 119 | + const response = await axios.get(get_didUrl); |
| 120 | + return response.data; |
| 121 | + } catch (e) { |
| 122 | + const { response } = e; |
| 123 | + return response.data; |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments