Skip to content

Commit

Permalink
feat(comp-api): enhance and review
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Feb 10, 2023
1 parent ed91385 commit 38b680e
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 61 deletions.
10 changes: 4 additions & 6 deletions uniquely/com-api/demo.http
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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}}
2 changes: 1 addition & 1 deletion uniquely/com-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {logger} from './config.js';
import {logger} from './lib/config.js';

import './route/home.js';
import './route/product/get.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {createLogger} from '@alwatr/logger';
import {TokenGeneratorConfig} from '@alwatr/token';

export const logger = createLogger('com-api');

Expand All @@ -9,15 +10,18 @@ export const config = {
token: process.env.ORDER_STORAGE_TOKEN ?? 'YOUR_SECRET_TOKEN',
productStorageName: process.env.PRODUCT_STORAGE_NAME ?? 'product',
},
token: {
token: <TokenGeneratorConfig>{
secret: process.env.SECRET ?? 'YOUR_SECRET',
algorithm: 'sha256',
encoding: 'base64url',
duration: null,
},
nanoServer: {
host: process.env.HOST ?? '0.0.0.0',
port: process.env.PORT != null ? +process.env.PORT : 8000,
accessToken: process.env.ACCESS_TOKEN ?? 'YOUR_SECRET_TOKEN',
allowAllOrigin: true,
},
};
} as const;

logger.logProperty('config', config);
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion uniquely/com-api/src/lib/storage.ts
Original file line number Diff line number Diff line change
@@ -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);
5 changes: 5 additions & 0 deletions uniquely/com-api/src/lib/token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {AlwatrTokenGenerator} from '@alwatr/token';

import {config} from './config.js';

export const tokenGenerator = new AlwatrTokenGenerator(config.token);
22 changes: 9 additions & 13 deletions uniquely/com-api/src/route/get-order.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
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<AlwatrServiceResponse> {
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) => {
return tokenGenerator.verify(params.userId, token) === 'valid';
});

return await storageClient.getStorage<Order>(params.userId);
}
});
15 changes: 6 additions & 9 deletions uniquely/com-api/src/route/get-product.ts
Original file line number Diff line number Diff line change
@@ -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<AlwatrServiceResponse> {
logger.logMethod('getProduct');
nanoServer.route('GET', '/product/', async () => {
logger.logMethod('get-product');
return await storageClient.getStorage<Product>(config.storage.productStorageName);
}
});
2 changes: 1 addition & 1 deletion uniquely/com-api/src/route/home.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {nanoServer} from '../lib/nano-server.js';
import {nanoServer} from '../lib/server.js';

nanoServer.route('GET', '/', () => ({
ok: true,
Expand Down
37 changes: 20 additions & 17 deletions uniquely/com-api/src/route/put-order.ts
Original file line number Diff line number Diff line change
@@ -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<AlwatrServiceResponse> {
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<Order>();
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>();

order.id = 'auto_increment';
order.status = 'registered';
order.clientId = clientId;
order.remoteAddress = remoteAddress;

return {
ok: true,
data: await storageClient.set<Order>(order, params.userId),
};
}
});
10 changes: 0 additions & 10 deletions uniquely/com-api/src/token.ts

This file was deleted.

0 comments on commit 38b680e

Please sign in to comment.