Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | 1x 1x 1x 1x 1x 1x 1x 1x | import { SchemaDataInput, SchemaDataResponse } from "../components/views/Schemas/types"; import { config, links } from "../config"; import { createMockApiRequest } from "../utils/helpers"; import { JwtUserData } from "../types"; // @TODO/toby Temp dummy data for demoing, sorry const DUMMY_SCHEMA_SAVES: { [key: string]: number } = { "serto-organization-profile": 3, "serto-team-member": 5, "consen-sys-employee": 3, "twitter-verify": 2, "consen-sys-retreat-2021": 4, "consen-sys-mesh-portfolio-company": 1, "consen-sys-hackathon-2021-participant": 3, "vaccination-certificate": 8, "immunoglobulin-detection-test-card": 4, "diploma-credential": 3, "tachyon-portfolio-company": 1, }; export class SertoSchemasService { private url; private jwt?: string; public userData?: JwtUserData; public isAuthenticated?: boolean; /** Path (or URL) to send user to in order for them to create a schema. */ public createSchemaPath: string; constructor(url?: string, jwt?: string, userData?: JwtUserData, createSchemaPath?: string) { this.url = url || config.SCHEMAS_API_URL; this.jwt = jwt; this.userData = userData; this.isAuthenticated = !!(jwt && userData); this.createSchemaPath = createSchemaPath || links.CREATE_SCHEMA_PATH; } /** Build URL at which a given schema will be hosted. */ public buildSchemaUrl = (slug: string, type: string, version?: string): string => { return `${this.url}/v1/public/${slug}${version && "/" + version}/${type}.json`; }; public async getSchemas(userCreated?: boolean): Promise<SchemaDataResponse[]> { let schemas = await this.request(`/v1/${userCreated ? "currentUser" : ""}`, "GET", undefined, userCreated); // @TODO/tobek This handles API bug where sometimes there is a wrapper object with schema inside `schema` property - remove when fixed. if (schemas?.[0]?.schema) { schemas = schemas.map((schema: any) => schema.schema); } schemas.forEach((schema: SchemaDataResponse) => { schema.favoriteCount = (schema.favoriteCount || 0) + (DUMMY_SCHEMA_SAVES[schema.slug] || 0); }); return schemas; } public async getSchema(slug: string): Promise<SchemaDataResponse> { if (!slug) { throw new Error("API error: Must provide a schema ID"); } let schema = await this.request(`/v1/public/${slug}`, "GET"); // @TODO/tobek This handles API bug where sometimes there is a wrapper object with schema inside `schema` property - remove when fixed. schema = schema?.schema || schema; schema.favoriteCount = (schema.favoriteCount || 0) + (DUMMY_SCHEMA_SAVES[schema.slug] || 0); return schema; } public async createSchema(schema: SchemaDataInput): Promise<any> { return this.request("/v1/", "POST", schema, true); } public async updateSchema(schema: SchemaDataInput): Promise<any> { return this.request( `/v1/${schema.slug}/update`, "POST", { ...schema, slug: undefined, // @TODO/tobek API errors if slug is included in update request even if slug hasn't been updated - should update API so that if slug is included but not changed then it's fine }, true, ); } public async toggleSaveSchema(slug: string): Promise<any> { return this.request(`/v1/${slug}/favorite`, "POST"); } private async request( path: string, method: "GET" | "DELETE" | "POST" = "GET", body?: any, authenticated?: boolean, ): Promise<any> { if (authenticated) { this.ensureAuthenticated(); } const headers: any = {}; if (this.jwt) { headers.authorization = `Bearer ${this.jwt}`; } if (body) { headers["Content-Type"] = "application/json"; } const response = await fetch(`${this.url}${path}`, { method, headers, body: JSON.stringify(body), }); const responseIsJson = response.headers.get("content-type")?.indexOf("application/json") === 0; if (!response.ok) { let errorMessage; if (responseIsJson) { const errorJson = await response.json(); if (errorJson?.error?.message) { errorMessage = errorJson.error.message; if (errorJson.error.code) { errorMessage += ` (${errorJson.error.code})`; } } else { errorMessage = JSON.stringify(errorJson); } } else { errorMessage = await response.text(); } console.error("API error", response.status, errorMessage); throw new Error("API error: " + errorMessage); } if (responseIsJson) { try { return await response.json(); } catch (err) { if (response.headers.get("content-length") === "0") { throw new Error('API error: API returned invalid JSON: ""'); } throw err; } } else { return await response.text(); } } private ensureAuthenticated() { if (!this.jwt) { throw new Error("Not authenticated"); } } } export const mockSertoSchemasService = { createSchemaPath: "/schemas/", buildSchemaUrl: (slug: string, type: string, version?: string): string => `https://example.com/schemas/${slug}${version && "/" + version}/${type}.json`, createSchema: createMockApiRequest(), updateSchema: createMockApiRequest(), getSchemas: createMockApiRequest([]), getSchema: createMockApiRequest({}), toggleSaveSchema: createMockApiRequest({}), }; |