Dwayne Harris
5 years ago
9 changed files with 2883 additions and 61 deletions
-
17.vscode/launch.json
-
2696package-lock.json
-
6package.json
-
7src/lib/authentication.ts
-
14src/lib/errors.ts
-
97src/plugins/api/authentication.ts
-
15src/plugins/api/index.ts
-
90src/plugins/api/profile.ts
-
2src/types/collections.ts
@ -0,0 +1,17 @@ |
|||
{ |
|||
// Use IntelliSense to learn about possible attributes. |
|||
// Hover to view descriptions of existing attributes. |
|||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 |
|||
"version": "0.2.0", |
|||
"configurations": [ |
|||
{ |
|||
"type": "node", |
|||
"request": "launch", |
|||
"name": "Launch Program", |
|||
"program": "${workspaceFolder}\\dist\\server.js", |
|||
"outFiles": [ |
|||
"${workspaceFolder}/**/*.js" |
|||
] |
|||
} |
|||
] |
|||
} |
2696
package-lock.json
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
@ -0,0 +1,90 @@ |
|||
import fastify, { |
|||
Plugin, |
|||
DefaultQuery, |
|||
DefaultParams, |
|||
RouteShorthandOptions, |
|||
} from 'fastify' |
|||
|
|||
import { Server, IncomingMessage, ServerResponse } from 'http' |
|||
import { unauthorizedError, serverError } from '../../lib/errors' |
|||
import { containerFor } from '../../lib/database' |
|||
import { IUser } from '../../types/collections' |
|||
|
|||
interface PluginOptions { |
|||
|
|||
} |
|||
|
|||
function updateRoute(server: fastify.FastifyInstance<Server, IncomingMessage, ServerResponse>) { |
|||
interface Headers { |
|||
authorization: string |
|||
} |
|||
|
|||
interface Body { |
|||
name?: string |
|||
about?: string |
|||
} |
|||
|
|||
const options: RouteShorthandOptions = { |
|||
schema: { |
|||
headers: { |
|||
type: 'object', |
|||
properties: { |
|||
authorization: { type: 'string' }, |
|||
}, |
|||
}, |
|||
body: { |
|||
type: 'object', |
|||
properties: { |
|||
name: { type: 'string' }, |
|||
about: { type: 'string' }, |
|||
}, |
|||
}, |
|||
response: { |
|||
200: { |
|||
type: 'object', |
|||
properties: { |
|||
id: { type: 'string' }, |
|||
name: { type: 'string' }, |
|||
}, |
|||
}, |
|||
}, |
|||
}, |
|||
} |
|||
|
|||
server.put<DefaultQuery, DefaultParams, Headers, Body>('/api/self', options, async (request, reply) => { |
|||
if (!request.user) return unauthorizedError(reply) |
|||
|
|||
const container = await containerFor(server.database.client, 'Users') |
|||
const userItem = await container.item(request.user.id, request.user.id) |
|||
const { resource: user } = await userItem.read<IUser>() |
|||
|
|||
if (!user) return serverError(reply) |
|||
|
|||
if (request.body.name) { |
|||
const name = request.body.name.trim() |
|||
if (name !== '') { |
|||
user.name = name |
|||
} |
|||
} |
|||
|
|||
if (request.body.about) { |
|||
const about = request.body.about.trim() |
|||
if (about !== '') { |
|||
user.about = about |
|||
} |
|||
} |
|||
|
|||
await userItem.replace<IUser>(user) |
|||
|
|||
return { |
|||
id: user.id, |
|||
name: user.name, |
|||
} |
|||
}) |
|||
} |
|||
|
|||
const plugin: Plugin<Server, IncomingMessage, ServerResponse, PluginOptions> = async server => { |
|||
updateRoute(server) |
|||
} |
|||
|
|||
export default plugin |
Write
Preview
Loading…
Cancel
Save
Reference in new issue