From 70c14045b710092bef18c7ad45dbf9525ce5dbd8 Mon Sep 17 00:00:00 2001 From: Dwayne Harris Date: Thu, 10 Oct 2019 22:59:30 -0400 Subject: [PATCH] WIP --- src/plugins/api/apps.ts | 111 ++++++++++++++++++++++++++++++++++++++- src/plugins/api/index.ts | 2 + src/schemas.ts | 10 ++++ 3 files changed, 122 insertions(+), 1 deletion(-) diff --git a/src/plugins/api/apps.ts b/src/plugins/api/apps.ts index 76ce47c..d702617 100644 --- a/src/plugins/api/apps.ts +++ b/src/plugins/api/apps.ts @@ -9,6 +9,7 @@ import { } from 'fastify' import { Server, IncomingMessage, ServerResponse } from 'http' +import pick from 'lodash/pick' import { appSchema, errorSchema } from '../../schemas' import { getUsers } from '../../lib/collections' @@ -16,11 +17,59 @@ import { generateString } from '../../lib/crypto' import { containerFor, getItem, normalize } from '../../lib/database' import { unauthorizedError, serverError, badRequestError, notFoundError } from '../../lib/errors' -import { APP_PARTITION_KEY } from '../../constants' +import { APP_PARTITION_KEY, MAX_NAME_LENGTH } from '../../constants' import { App, User } from '../../types/collections' import { PluginOptions } from '../../types' +function availabilityRoute(server: FastifyInstance) { + interface Body { + name: string + } + + const options: RouteShorthandOptions = { + schema: { + body: { + type: 'object', + required: ['name'], + properties: { + name: { + type: 'string', + maxLength: MAX_NAME_LENGTH, + }, + }, + }, + response: { + 200: { + type: 'object', + properties: { + id: { type: 'string' }, + available: { type: 'boolean' }, + }, + }, + 400: errorSchema, + }, + }, + } + + server.post('/api/app/available', options, async (request, reply) => { + if (!server.database) return serverError(reply) + + const id = normalize(request.body.name) + + const app = await getItem({ + container: containerFor(server.database.client, 'Apps'), + partitionKey: APP_PARTITION_KEY, + id, + }) + + return { + id, + available: !app, + } + }) +} + function appsRoute(server: FastifyInstance) { interface Query { sort?: string @@ -299,6 +348,12 @@ function updateRoute(server: FastifyInstance) { + interface Params { + id: string + } + + const options: RouteShorthandOptions = { + schema: { + params: { + type: 'object', + properties: { + id: { type: 'string' }, + }, + }, + response: { + 200: appSchema, + 400: errorSchema, + } + }, + } + + server.get('/api/app/:id', options, async (request, reply) => { + if (!server.database) return serverError(reply) + + const app = await getItem({ + container: containerFor(server.database.client, 'Apps'), + id: request.params.id, + partitionKey: APP_PARTITION_KEY, + }) + + if (!app) return notFoundError(reply) + + const user = await getItem({ + container: containerFor(server.database.client, 'Users'), + id: app.userId, + }) + + if (request.viewer && request.viewer.id === app.userId) { + const attributes = ['id', 'version', 'name', 'imageUrl', 'coverImageUrl', 'iconImageUrl', 'about', 'websiteUrl', 'companyName', 'rating', 'updated', 'created', 'publicKey', 'privateKey', 'revisions'] + return { + ...pick(app, attributes), + user, + } + } else { + const attributes = ['id', 'version', 'name', 'imageUrl', 'coverImageUrl', 'iconImageUrl', 'about', 'websiteUrl', 'companyName', 'rating', 'updated', 'created'] + return { + ...pick(app, attributes), + user, + } + } + }) +} + const plugin: Plugin = async server => { + availabilityRoute(server) appsRoute(server) selfAppsRoute(server) createRoute(server) updateRoute(server) + getRoute(server) } export default plugin diff --git a/src/plugins/api/index.ts b/src/plugins/api/index.ts index 5e98a09..b39e65e 100644 --- a/src/plugins/api/index.ts +++ b/src/plugins/api/index.ts @@ -6,6 +6,7 @@ import { Server, IncomingMessage, ServerResponse } from 'http' import { JWT } from '../../lib/crypto' import { tokenFromHeader } from '../../lib/http' +import apps from './apps' import authentication from './authentication' import groups from './groups' import posts from './posts' @@ -77,6 +78,7 @@ const plugin: Plugin = a reply.send(response) }) + server.register(apps) server.register(authentication) server.register(groups) server.register(posts) diff --git a/src/schemas.ts b/src/schemas.ts index 77aa809..616ce8f 100644 --- a/src/schemas.ts +++ b/src/schemas.ts @@ -59,7 +59,17 @@ export const appSchema: JSONSchema = { websiteUrl: { type: 'string' }, companyName: { type: 'string' }, version: { type: 'string' }, + updated: { type: 'number' }, created: { type: 'number' }, + + publicKey: { type: 'string' }, + privateKey: { type: 'string' }, + revisions: { + type: 'array', + items: { + type: 'object', + }, + }, } }