diff --git a/uniquely/com-api/demo.http b/uniquely/com-api/demo.http index 0f2642200..9903dfe8e 100644 --- a/uniquely/com-api/demo.http +++ b/uniquely/com-api/demo.http @@ -9,14 +9,12 @@ GET {{apiUrl}}/ ### Get all product Get {{apiUrl}}/product/ -### New order -PUT {{apiUrl}}/order/ +### New order of user +PUT {{apiUrl}}/order/?userId=abc123 Authorization: Bearer {{userToken}} Content-Type: application/json { - "id": "abc123-101", - "userId": "abc123", "itemList": [ { "productId": "12345", @@ -42,6 +40,6 @@ Content-Type: application/json "finalPrice": 900000 } -### Get user order list -get {{apiUrl}}/order/?userId=njfamirm +### Get all orders list of special user +get {{apiUrl}}/order/?userId=abc123 Authorization: Bearer {{userToken}} diff --git a/uniquely/com-api/src/index.ts b/uniquely/com-api/src/index.ts index 836e8a9fb..03f73c88d 100644 --- a/uniquely/com-api/src/index.ts +++ b/uniquely/com-api/src/index.ts @@ -1,4 +1,4 @@ -import {logger} from './config.js'; +import {logger} from './lib/config.js'; import './route/home.js'; import './route/product/get.js'; diff --git a/uniquely/com-api/src/config.ts b/uniquely/com-api/src/lib/config.ts similarity index 81% rename from uniquely/com-api/src/config.ts rename to uniquely/com-api/src/lib/config.ts index 42dd71b39..5b493f624 100644 --- a/uniquely/com-api/src/config.ts +++ b/uniquely/com-api/src/lib/config.ts @@ -1,4 +1,5 @@ import {createLogger} from '@alwatr/logger'; +import {TokenGeneratorConfig} from '@alwatr/token'; export const logger = createLogger('com-api'); @@ -9,8 +10,11 @@ export const config = { token: process.env.ORDER_STORAGE_TOKEN ?? 'YOUR_SECRET_TOKEN', productStorageName: process.env.PRODUCT_STORAGE_NAME ?? 'product', }, - token: { + token: { secret: process.env.SECRET ?? 'YOUR_SECRET', + algorithm: 'sha256', + encoding: 'base64url', + duration: null, }, nanoServer: { host: process.env.HOST ?? '0.0.0.0', @@ -18,6 +22,6 @@ export const config = { accessToken: process.env.ACCESS_TOKEN ?? 'YOUR_SECRET_TOKEN', allowAllOrigin: true, }, -}; +} as const; logger.logProperty('config', config); diff --git a/uniquely/com-api/src/lib/nano-server.ts b/uniquely/com-api/src/lib/server.ts similarity index 76% rename from uniquely/com-api/src/lib/nano-server.ts rename to uniquely/com-api/src/lib/server.ts index 593467ef5..df789f689 100644 --- a/uniquely/com-api/src/lib/nano-server.ts +++ b/uniquely/com-api/src/lib/server.ts @@ -1,5 +1,5 @@ import {AlwatrNanoServer} from '@alwatr/nano-server'; -import {config} from '../config.js'; +import {config} from './config.js'; export const nanoServer = new AlwatrNanoServer(config.nanoServer); diff --git a/uniquely/com-api/src/lib/storage.ts b/uniquely/com-api/src/lib/storage.ts index 52f8dee88..841a0e0d7 100644 --- a/uniquely/com-api/src/lib/storage.ts +++ b/uniquely/com-api/src/lib/storage.ts @@ -1,5 +1,5 @@ import {AlwatrStorageClient} from '@alwatr/storage-client'; -import {config} from '../config.js'; +import {config} from './config.js'; export const storageClient = new AlwatrStorageClient(config.storage); diff --git a/uniquely/com-api/src/lib/token.ts b/uniquely/com-api/src/lib/token.ts new file mode 100644 index 000000000..590e21384 --- /dev/null +++ b/uniquely/com-api/src/lib/token.ts @@ -0,0 +1,5 @@ +import {AlwatrTokenGenerator} from '@alwatr/token'; + +import {config} from './config.js'; + +export const tokenGenerator = new AlwatrTokenGenerator(config.token); diff --git a/uniquely/com-api/src/route/get-order.ts b/uniquely/com-api/src/route/get-order.ts index a3efe305f..c76ec66ed 100644 --- a/uniquely/com-api/src/route/get-order.ts +++ b/uniquely/com-api/src/route/get-order.ts @@ -1,17 +1,13 @@ -import {logger} from '../../config.js'; -import {nanoServer} from '../../lib/nano-server.js'; -import {storageClient} from '../../lib/storage.js'; -import {tokenGenerator} from '../../token.js'; +import {logger} from '../lib/config.js'; +import {nanoServer} from '../lib/server.js'; +import {storageClient} from '../lib/storage.js'; +import {tokenGenerator} from '../lib/token.js'; -import type {AlwatrConnection} from '@alwatr/nano-server'; -import type {AlwatrServiceResponse} from '@alwatr/type'; -import type {Order} from '@alwatr/type/src/customer-order-management.js'; +import type {Order} from '@alwatr/type/customer-order-management.js'; -// Get current order object -nanoServer.route('GET', '/order/', getOrder); - -async function getOrder(connection: AlwatrConnection): Promise { - logger.logMethod('getOrder'); +// Get all orders of special customer +nanoServer.route('GET', '/order/', async (connection) => { + logger.logMethod('get-order'); const params = connection.requireQueryParams<{userId: string}>({userId: 'string'}); connection.requireToken((token: string) => { @@ -19,4 +15,4 @@ async function getOrder(connection: AlwatrConnection): Promise(params.userId); -} +}); diff --git a/uniquely/com-api/src/route/get-product.ts b/uniquely/com-api/src/route/get-product.ts index cc8822681..de6e12182 100644 --- a/uniquely/com-api/src/route/get-product.ts +++ b/uniquely/com-api/src/route/get-product.ts @@ -1,13 +1,10 @@ -import {config, logger} from '../../config.js'; -import {nanoServer} from '../../lib/nano-server.js'; -import {storageClient} from '../../lib/storage.js'; +import {config, logger} from '../lib/config.js'; +import {nanoServer} from '../lib/server.js'; +import {storageClient} from '../lib/storage.js'; -import type {AlwatrServiceResponse} from '@alwatr/type'; import type {Product} from '@alwatr/type/src/customer-order-management.js'; -nanoServer.route('GET', '/product/', getProduct); - -async function getProduct(): Promise { - logger.logMethod('getProduct'); +nanoServer.route('GET', '/product/', async () => { + logger.logMethod('get-product'); return await storageClient.getStorage(config.storage.productStorageName); -} +}); diff --git a/uniquely/com-api/src/route/home.ts b/uniquely/com-api/src/route/home.ts index cf754a07f..cc4ad07e6 100644 --- a/uniquely/com-api/src/route/home.ts +++ b/uniquely/com-api/src/route/home.ts @@ -1,4 +1,4 @@ -import {nanoServer} from '../lib/nano-server.js'; +import {nanoServer} from '../lib/server.js'; nanoServer.route('GET', '/', () => ({ ok: true, diff --git a/uniquely/com-api/src/route/put-order.ts b/uniquely/com-api/src/route/put-order.ts index ff8aff8e0..1a78f26f3 100644 --- a/uniquely/com-api/src/route/put-order.ts +++ b/uniquely/com-api/src/route/put-order.ts @@ -1,35 +1,38 @@ -import {logger} from '../../config.js'; -import {nanoServer} from '../../lib/nano-server.js'; -import {storageClient} from '../../lib/storage.js'; -import {tokenGenerator} from '../../token.js'; +import {logger} from '../lib/config.js'; +import {nanoServer} from '../lib/server.js'; +import {storageClient} from '../lib/storage.js'; +import {tokenGenerator} from '../lib/token.js'; -import type {AlwatrConnection} from '@alwatr/nano-server'; -import type {AlwatrServiceResponse} from '@alwatr/type'; import type {Order} from '@alwatr/type/customer-order-management.js'; -// Add order -nanoServer.route('PUT', '/order/', newOrder); - -async function newOrder(connection: AlwatrConnection): Promise { - logger.logMethod('newOrder'); +// Insert new order +nanoServer.route('PUT', '/order/', async (connection) => { + logger.logMethod('put-order'); const params = connection.requireQueryParams<{userId: string}>({userId: 'string'}); const token = connection.requireToken((token: string) => { return tokenGenerator.verify(params.userId, token) === 'valid'; }); - const order = await connection.requireJsonBody(); + const remoteAddress = connection.incomingMessage.socket.remoteAddress ?? 'unknown'; + const clientId = connection.incomingMessage.headers['client-id']; - if (await storageClient.has(order.id, token)) { + if (!clientId) { return { ok: false, - statusCode: 400, - errorCode: 'order_exist', + statusCode: 401, + errorCode: 'client_id_header_required', }; } - // else + const order = await connection.requireJsonBody(); + + order.id = 'auto_increment'; + order.status = 'registered'; + order.clientId = clientId; + order.remoteAddress = remoteAddress; + return { ok: true, data: await storageClient.set(order, params.userId), }; -} +}); diff --git a/uniquely/com-api/src/token.ts b/uniquely/com-api/src/token.ts deleted file mode 100644 index 59be77dde..000000000 --- a/uniquely/com-api/src/token.ts +++ /dev/null @@ -1,10 +0,0 @@ -import {AlwatrTokenGenerator} from '@alwatr/token'; - -import {config} from './config.js'; - -export const tokenGenerator = new AlwatrTokenGenerator({ - secret: config.token.secret, - duration: null, - algorithm: 'sha256', - encoding: 'base64url', -});