Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add req,res type #31

Merged
merged 4 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/modules/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Request } from "express";
import ModuleBase from "./moduleBase";
import DepositService from "./service/depositService";
import UseService from "./service/useService";
import { RESPONSE_STATUS } from "../types/type";
import { RESPONSE_STATUS, request, response } from "../types/type";

export default class Admin extends ModuleBase {
private depositService: DepositService;
Expand Down Expand Up @@ -50,27 +50,31 @@ export default class Admin extends ModuleBase {
* @returns Result of transaction.
*/
async writeResponse(req:Request, amount: number, responseData: string, status: RESPONSE_STATUS ) {
const appName = req.body.valuePath[1];
const serviceName = req.body.valuePath[3];
const requesterAddress = req.body.auth.addr;
const requestKey = req.body.valuePath[5];
return await this.useService.writeResponse(status , appName, serviceName, requesterAddress, requestKey, responseData, amount);
const requestData = this.getDataFromServiceRequest(req);
const response: response = {
status: status,
amount: amount,
responseData: responseData,
...requestData,
}
return await this.useService.writeResponse(response);
}
/**
* Get data from service request. You should use it only with service trigger.
* @param {Request} request - Request data from request trigger. If req data is not from trigger function, it will throw error.
* @returns Object with appName, serviceName, requesterAddress, requestKey, responseData.
* @returns RequestData type.
*/
getDataFromServiceRequest(req: Request) {
if(!req.body.valuePath[1] || !req.body.valuePath[3] || !req.body.valuePath[5] || !req.body.value.prompt) {
throw new Error("Not from service request");
}
return {
const requestData: request = {
appName: req.body.valuePath[1],
serviceName: req.body.valuePath[3],
requesterAddress: req.body.auth.addr,
requestKey: req.body.valuePath[5],
responseData: req.body.value.prompt,
requestData: req.body.value.prompt,
}
return requestData;
}
}
}
5 changes: 3 additions & 2 deletions src/modules/service/useService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SetOperation } from "@ainblockchain/ain-js/lib/types";
import { Path } from "../../constants";
import { HISTORY_TYPE, RESPONSE_STATUS } from "../../types/type";
import { HISTORY_TYPE, RESPONSE_STATUS, response } from "../../types/type";
import { buildSetOperation } from "../../utils/builder";
import ServiceBase from "./serviceBase";

Expand Down Expand Up @@ -40,7 +40,8 @@ export default class UseService extends ServiceBase{
return amount;
}

async writeResponse(status: RESPONSE_STATUS, appName: string, serviceName: string, requesterAddress: string, requestKey: string, responseData: string, amount: number) {
async writeResponse(response: response) {
const { responseData, status, requesterAddress, requestKey, appName, serviceName, amount } = response;
const responsePath = Path.app(appName).response(serviceName, requesterAddress, requestKey);
const responseValue = {
status,
Expand Down
27 changes: 23 additions & 4 deletions src/types/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export type setRuleParam = {
} & writeRuleConfig;

export type serviceBillingConfig = {
costPerToken: number;
minCost: number;
maxCost?: number;
responseTimeout?: number;
costPerToken: number;
minCost: number;
maxCost?: number;
responseTimeout?: number;
}

export type appBillingConfig = {
Expand Down Expand Up @@ -54,3 +54,22 @@ export enum RESPONSE_STATUS {
SUCCESS = "SUCCESS",
FAIL = "FAIL",
}

export type request = {
requestData: string,
requesterAddress: string,
requestKey: string,
serviceName: string,
appName: string,
};

export type response = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor)

export type response = request & {
  responseData: string,
  status: RESPONSE_STATUS,
  amount: number,
};

로 연관성을 명확하게 표기 가능할 것 같습니다.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor) amount 대신 cost 어떠신가요.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반영했습니다.

responseData: string,
amount: number,
status: RESPONSE_STATUS,
requestData: string,
requesterAddress: string,
requestKey: string,
appName: string,
serviceName: string,
}