generated from axone-protocol/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: created historical-price endpoint
- Loading branch information
1 parent
5fbb8b4
commit 9fc9825
Showing
25 changed files
with
5,881 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const config = { | ||
osmosis: { | ||
url: process.env.OSMOSIS_BASE_URL, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export enum HttpErrorMessage { | ||
BAD_REQUEST = 'Bad Request', | ||
UNAUTHORIZED = 'Unauthorized', | ||
FORBIDDEN = 'Forbidden', | ||
NOT_FOUND = 'Not Found', | ||
INTERNAL_SERVER_ERROR = 'Internal Server Error', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export enum HttpStatus { | ||
OK = 200, | ||
CREATED = 201, | ||
ACCEPTED = 202, | ||
BAD_REQUEST = 400, | ||
UNAUTHORIZED = 401, | ||
FORBIDDEN = 403, | ||
NOT_FOUND = 404, | ||
INTERNAL_SERVER_ERROR = 500, | ||
NOT_IMPLEMENTED = 501, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export enum Endpoints { | ||
HISTORICAL_PRICE = 'tokens/v2/historical/:symbol/chart', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export enum RouteParam { | ||
SYMBOL = ':symbol', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* eslint-disable @typescript-eslint/no-namespace, @typescript-eslint/no-explicit-any */ | ||
import { GetHistoricalChartDto } from '@/app/api/historical-price/dtos/get-historical-chart.dto'; | ||
import { config } from '@core/config/config'; | ||
import { Endpoints } from '@core/services/osmosis/enums/endpoints.enum'; | ||
import { RouteParam } from '@core/services/osmosis/enums/route-param.enum'; | ||
import { FailedResponse } from '@core/services/osmosis/responses/failed.response'; | ||
import { GSFResponse } from '@core/services/osmosis/responses/generic-success-failed.response'; | ||
import { BadRequestException } from '@core/utils/bad-request-exception'; | ||
import { createUrlParams } from '@core/utils/create-url-params'; | ||
import { HttpRequester } from '@core/utils/http-requester'; | ||
import { HistoricalChartRes } from './responses/historical-chart.response'; | ||
|
||
export namespace OsmosisService { | ||
const BASE_URL = config.osmosis.url; | ||
|
||
function constructUrl (endpoint: string, params?: string): string { | ||
return `${BASE_URL}/${endpoint}${params ? `?${params}` : ''}`; | ||
} | ||
|
||
export async function getHistoricalChart ( | ||
payload: GetHistoricalChartDto | ||
): Promise<HistoricalChartRes> { | ||
const endpoint = Endpoints.HISTORICAL_PRICE.replace( | ||
RouteParam.SYMBOL, | ||
payload.symbol | ||
); | ||
|
||
return errorHandleWrapper( | ||
HttpRequester.get.bind( | ||
null, | ||
constructUrl(endpoint, createUrlParams({ tf: payload.range })) | ||
) | ||
); | ||
} | ||
|
||
async function errorHandleWrapper<T> (fn: any): Promise<T> { | ||
try { | ||
const response: GSFResponse<T> = await fn(); | ||
|
||
if (isFailedResponse(response)) { | ||
throw new BadRequestException(response.message); | ||
} | ||
|
||
return response as T; | ||
} catch (e: any) { | ||
throw new BadRequestException(e.message); | ||
} | ||
} | ||
|
||
function isFailedResponse<T> ( | ||
response: GSFResponse<T> | ||
): response is FailedResponse { | ||
return (response as FailedResponse).message !== undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type FailedResponse = { | ||
message: string; | ||
} |
3 changes: 3 additions & 0 deletions
3
app/api/core/services/osmosis/responses/generic-success-failed.response.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { FailedResponse } from '@core/services/osmosis/responses/failed.response'; | ||
|
||
export type GSFResponse<T> = T | FailedResponse; // Generic Success / Failed Response for Osmosis |
10 changes: 10 additions & 0 deletions
10
app/api/core/services/osmosis/responses/historical-chart.response.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export type HistoricalChartRes = HistoricalChartItem[]; | ||
|
||
export type HistoricalChartItem = { | ||
time: number; | ||
close: number; | ||
high: number; | ||
low: number; | ||
open: number; | ||
volume: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { HttpStatus } from '@core/enums/http-status.enum'; | ||
import { HttpException } from '@core/utils/http-exception'; | ||
|
||
export class BadRequestException extends HttpException { | ||
constructor (message: string) { | ||
super(message, HttpStatus.BAD_REQUEST); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function createUrlParams (params: any): string { | ||
const url = new URLSearchParams(params); | ||
return url.toString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { NextResponse } from 'next/server'; | ||
|
||
import { HttpErrorMessage } from '@core/enums/http-error-message.enum'; | ||
import { HttpStatus } from '@core/enums/http-status.enum'; | ||
|
||
export async function exceptionFilter (fn: any) { | ||
try { | ||
return await fn(); // just to catch all error at this step | ||
} catch (e: any) { | ||
return NextResponse.json( | ||
{ | ||
error: e?.message || HttpErrorMessage.INTERNAL_SERVER_ERROR, | ||
status: e?.statusCode || HttpStatus.INTERNAL_SERVER_ERROR, | ||
}, | ||
{ status: e?.statusCode || HttpStatus.INTERNAL_SERVER_ERROR } | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { HttpStatus } from '@core/enums/http-status.enum'; | ||
|
||
export class HttpException extends Error { | ||
private statusCode: HttpStatus; | ||
|
||
constructor (message: string, statusCode: HttpStatus) { | ||
super(message); | ||
this.statusCode = statusCode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* eslint-disable @typescript-eslint/no-namespace */ | ||
|
||
export namespace HttpRequester { | ||
const enum HttpMethod { | ||
GET = 'get', | ||
POST = 'post', | ||
PUT = 'put', | ||
PATCH = 'patch', | ||
DELETE = 'delete', | ||
HEAD = 'head', | ||
OPTIONS = 'options', | ||
} | ||
|
||
export async function get<T> ( | ||
url: string, | ||
headers?: HeadersInit, | ||
revalidate?: number | ||
): Promise<T> { | ||
const res = await fetch(url, { | ||
method: HttpMethod.GET, | ||
headers, | ||
next: { | ||
revalidate, | ||
}, | ||
}); | ||
|
||
return res.json(); | ||
} | ||
|
||
export async function post<T> ( | ||
url: string, | ||
body: BodyInit, | ||
headers?: HeadersInit | ||
): Promise<T> { | ||
const res = await fetch(url, { | ||
method: HttpMethod.POST, | ||
body, | ||
headers, | ||
}); | ||
|
||
return res.json(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { NextRequest } from 'next/server'; | ||
|
||
type QueryParams<T> = { | ||
[key: string]: T; | ||
} | ||
|
||
export function parseUrlParams (request: NextRequest) { | ||
const url = new URL(request.url); | ||
const queryParamsObject: QueryParams<string> = {}; | ||
|
||
url.searchParams.forEach((value, key) => { | ||
queryParamsObject[key] = value; | ||
}); | ||
|
||
return queryParamsObject; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { SafeParseError, ZodSchema } from 'zod'; | ||
|
||
import { BadRequestException } from '@core/utils/bad-request-exception'; | ||
|
||
export function schemaValidate (schema: ZodSchema, value: unknown) { | ||
const res = schema.safeParse(value); | ||
|
||
if (res.success) { | ||
return res.data; | ||
} | ||
|
||
throw new BadRequestException(errorView(res)); | ||
} | ||
|
||
function errorView (value: SafeParseError<unknown>): string { | ||
const flattenErrors = value.error.flatten(); | ||
let message = 'Passed fields didn\'t match current schema:'; | ||
|
||
for (const [key, value] of Object.entries(flattenErrors.fieldErrors)) { | ||
message += ` ${key} - [${value?.toString()}]`; | ||
} | ||
|
||
return message; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type GetHistoricalChartDto = { | ||
symbol: string; | ||
range: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
import { OsmosisService } from '@core/services/osmosis/osmosis.service'; | ||
import { HistoricalChartRes } from '@core/services/osmosis/responses/historical-chart.response'; | ||
import { exceptionFilter } from '@core/utils/exception-filter'; | ||
import { parseUrlParams } from '@core/utils/parse-url-params'; | ||
import { schemaValidate } from '@core/utils/schema-validate'; | ||
import { GetHistoricalChartDto } from '@historical-price/dtos/get-historical-chart.dto'; | ||
import { getHistoricalChartSchema } from '@historical-price/schemas/get-historical-chart.schema'; | ||
|
||
export async function GET (request: NextRequest) { | ||
return exceptionFilter(getHistoricalPrice.bind(null, request)); | ||
} | ||
|
||
async function getHistoricalPrice (request: NextRequest) { | ||
const dto: GetHistoricalChartDto = schemaValidate( | ||
getHistoricalChartSchema, | ||
parseUrlParams(request) | ||
); | ||
const historicalChart = await OsmosisService.getHistoricalChart(dto); | ||
return NextResponse.json(historicalPriceView(historicalChart)); | ||
} | ||
|
||
function historicalPriceView (res: HistoricalChartRes) { | ||
return res.map((item) => ({ | ||
time: item.time, | ||
price: item.close, | ||
})); | ||
} |
12 changes: 12 additions & 0 deletions
12
app/api/historical-price/schemas/get-historical-chart.schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { z } from 'zod'; | ||
|
||
export const getHistoricalChartSchema = z.object({ | ||
symbol: z.string(), | ||
range: z.string().transform((val) => { | ||
const parsedNumber = parseFloat(val); | ||
if (isNaN(parsedNumber)) { | ||
throw new Error('Invalid number format'); | ||
} | ||
return parsedNumber; | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,6 @@ | ||
type ChartData = { | ||
name: string; | ||
price: number; | ||
time: number; | ||
price: number; | ||
}; | ||
|
||
const mockChartData: ChartData[] = [ | ||
{ | ||
name: '01 Apr', | ||
price: 10.00, | ||
}, | ||
{ | ||
name: '04 Apr', | ||
price: 10.00, | ||
}, | ||
{ | ||
name: '06 Apr', | ||
price: 20.00, | ||
}, | ||
{ | ||
name: '08 Apr', | ||
price: 30.00, | ||
}, | ||
{ | ||
name: '10 Apr', | ||
price: 40.00, | ||
}, | ||
{ | ||
name: '14 Apr', | ||
price: 50.00, | ||
}, | ||
{ | ||
name: '16 Apr', | ||
price: 60.00, | ||
}, | ||
{ | ||
name: '18 Apr', | ||
price: 70.00, | ||
}, | ||
{ | ||
name: '20 Apr', | ||
price: 60.00, | ||
}, | ||
{ | ||
name: '22 Apr', | ||
price: 55.00, | ||
}, | ||
{ | ||
name: '28 Apr', | ||
price: 80.00, | ||
}, | ||
{ | ||
name: '30 Apr', | ||
price: 90.00, | ||
}, | ||
]; | ||
|
||
export { mockChartData }; | ||
export type { ChartData }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.