github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/kat/src/routers/asset-definitions.ts (about) 1 // Copyright © 2021 Kaleido, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 import { Router } from 'express'; 16 import { v4 as uuidV4 } from 'uuid'; 17 import RequestError from '../lib/request-handlers'; 18 import * as assetDefinitionsHandler from '../handlers/asset-definitions'; 19 import { constants } from '../lib/utils'; 20 import * as utils from '../lib/utils'; 21 import { config } from '../lib/config'; 22 23 const router = Router(); 24 25 router.get('/', async (req, res, next) => { 26 try { 27 const skip = Number(req.query.skip || 0); 28 const limit = Number(req.query.limit || constants.DEFAULT_PAGINATION_LIMIT); 29 if (isNaN(skip) || isNaN(limit)) { 30 throw new RequestError('Invalid skip / limit', 400); 31 } 32 res.send(await assetDefinitionsHandler.handleGetAssetDefinitionsRequest({}, skip, limit)); 33 } catch (err) { 34 next(err); 35 } 36 }); 37 38 router.get('/:assetDefinitionID', async (req, res, next) => { 39 try { 40 res.send(await assetDefinitionsHandler.handleGetAssetDefinitionRequest(req.params.assetDefinitionID)); 41 } catch (err) { 42 next(err); 43 } 44 }); 45 46 router.post('/search', async (req, res, next) => { 47 try { 48 const skip = Number(req.body.skip || 0); 49 const limit = Number(req.body.limit || constants.DEFAULT_PAGINATION_LIMIT); 50 if (req.body.count !== true && (isNaN(skip) || isNaN(limit))) { 51 throw new RequestError('Invalid skip / limit', 400); 52 } 53 if (!req.body.query) { 54 throw new RequestError('Missing search query', 400); 55 } 56 res.send(req.body.count === true ? 57 await assetDefinitionsHandler.handleCountAssetDefinitionsRequest(req.body.query) : 58 await assetDefinitionsHandler.handleGetAssetDefinitionsRequest(req.body.query, skip, limit) 59 ); 60 } catch (err) { 61 next(err); 62 } 63 }); 64 65 router.post('/', async (req, res, next) => { 66 try { 67 if (!req.body.name || req.body.name === '') { 68 throw new RequestError('Missing or invalid asset definition name', 400); 69 } 70 if (!utils.isAuthorValid(req.body.author, config.protocol)) { 71 throw new RequestError('Missing or invalid asset definition author', 400); 72 } 73 if (typeof req.body.isContentPrivate !== 'boolean') { 74 throw new RequestError('Missing asset definition content privacy', 400); 75 } 76 if (typeof req.body.isContentUnique !== 'boolean') { 77 throw new RequestError('Missing asset definition content uniqueness', 400); 78 } 79 let assetDefinitionID; 80 switch (config.protocol) { 81 case 'corda': 82 if (!req.body.assetDefinitionID) { 83 throw new RequestError('Missing asset definition id', 400); 84 } 85 assetDefinitionID = req.body.assetDefinitionID; 86 break; 87 case 'ethereum': 88 assetDefinitionID = uuidV4(); 89 break; 90 } 91 const sync = req.query.sync === 'true'; 92 await assetDefinitionsHandler.handleCreateAssetDefinitionRequest(assetDefinitionID, req.body.name, req.body.isContentPrivate, 93 req.body.isContentUnique, req.body.author, req.body.descriptionSchema, req.body.contentSchema, req.body.indexes, sync); 94 res.send({ status: sync ? 'success' : 'submitted', assetDefinitionID }); 95 } catch (err) { 96 next(err); 97 } 98 }); 99 100 export default router;