Commit 5f5e99cc authored by isstuev's avatar isstuev

common handler

parent 85a02bf2
import type { NextApiRequest, NextApiResponse } from 'next' import type { NextApiRequest } from 'next'
import fetch from 'pages/api/utils/fetch'; import handler from 'pages/api/utils/handler';
export default async function handler(_req: NextApiRequest, res: NextApiResponse) { const getUrl = (req: NextApiRequest) => `/account/v1/user/tags/address/${ req.query.id }`
const { id } = _req.query;
const url = `/account/v1/user/tags/address/${ id }`;
switch (_req.method) { const addressDeleteHandler = handler(getUrl, [ 'DELETE' ]);
case 'DELETE': {
const response = await fetch(url, { method: 'DELETE' });
// FIXME: add error handlers
if (response.status !== 200) {
// eslint-disable-next-line no-console
console.log(response.statusText);
}
res.status(200).end();
break;
}
default: { export default addressDeleteHandler;
res.setHeader('Allow', [ 'DELETE' ])
res.status(405).end(`Method ${ _req.method } Not Allowed`)
}
}
}
import type { NextApiRequest, NextApiResponse } from 'next' import handler from 'pages/api/utils/handler';
import fetch from 'pages/api/utils/fetch'; import type { AddressTags } from 'pages/api/types/account';
export default async function handler(_req: NextApiRequest, res: NextApiResponse) { const addressHandler = handler<AddressTags>(() => '/account/v1/user/tags/address', [ 'GET', 'POST' ]);
const url = '/account/v1/user/tags/address';
switch (_req.method) { export default addressHandler;
case 'GET': {
const response = await fetch(url)
const data = await response.json();
res.status(200).json(data)
break;
}
case 'POST': {
const response = await fetch(url, {
method: 'POST',
body: _req.body,
})
const data = await response.json();
res.status(200).json(data)
break;
}
default: {
res.setHeader('Allow', [ 'GET', 'POST' ])
res.status(405).end(`Method ${ _req.method } Not Allowed`)
}
}
}
import type { NextApiRequest, NextApiResponse } from 'next' import type { NextApiRequest } from 'next'
import fetch from 'pages/api/utils/fetch'; import handler from 'pages/api/utils/handler';
export default async function handler(_req: NextApiRequest, res: NextApiResponse) { const getUrl = (req: NextApiRequest) => `/account/v1/user/tags/transaction/${ req.query.id }`
const { id } = _req.query;
const url = `/account/v1/user/tags/transaction/${ id }`;
switch (_req.method) { const transactionDeleteHandler = handler(getUrl, [ 'DELETE' ]);
case 'DELETE': {
const response = await fetch(url, { method: 'DELETE' });
// FIXME: add error handlers
if (response.status !== 200) {
// eslint-disable-next-line no-console
console.log(response.statusText);
}
res.status(200).end();
break;
}
default: { export default transactionDeleteHandler;
res.setHeader('Allow', [ 'DELETE' ])
res.status(405).end(`Method ${ _req.method } Not Allowed`)
}
}
}
import type { NextApiRequest, NextApiResponse } from 'next' import handler from 'pages/api/utils/handler';
import fetch from 'pages/api/utils/fetch'; import type { TransactionTags } from 'pages/api/types/account';
export default async function handler(_req: NextApiRequest, res: NextApiResponse) { const transactionHandler = handler<TransactionTags>(() => '/account/v1/user/tags/transaction', [ 'GET', 'POST' ]);
const url = '/account/v1/user/tags/transaction';
switch (_req.method) { export default transactionHandler;
case 'GET': {
const response = await fetch(url)
const data = await response.json();
res.status(200).json(data)
break;
}
case 'POST': {
const response = await fetch(url, {
method: 'POST',
body: _req.body,
})
const data = await response.json();
res.status(200).json(data)
break;
}
default: {
res.setHeader('Allow', [ 'GET', 'POST' ])
res.status(405).end(`Method ${ _req.method } Not Allowed`)
}
}
}
// FIXME: here are types of the elixir api's responses
// and in types/api/ folder we have types of the node api's responses
// maybe they are always the same and there is no need to keep two separate files with types
export interface AddressTag {
address_hash: string;
name: string;
id: string;
}
export type AddressTags = Array<AddressTag>
export interface ApiKey {
apiKey: string;
apiKeyName: string;
}
export type ApiKeys = Array<ApiKey>
export interface ModelError {
message: string;
}
export interface NotificationDirection {
incoming: boolean;
outcoming: boolean;
}
export interface NotificationSettings {
_native?: NotificationDirection;
erc20?: NotificationDirection;
erc7211155?: NotificationDirection;
}
export interface Transaction {
fromAddressHash?: string;
toAddressHash?: string;
createdContractAddressHash?: string;
}
export interface TransactionTag {
transaction_hash: string;
name: string;
id: string;
}
export type TransactionTags = Array<TransactionTag>
export type Transactions = Array<Transaction>
export interface UserInfo {
name?: string;
nickname?: string;
email?: string;
}
export interface WatchlistAddress {
addressHash: string;
addressName: string;
addressBalance: number;
coinName: string;
exchangeRate?: number;
notificationSettings: NotificationSettings;
}
export interface WatchlistAddressNew {
addressName: string;
notificationSettings: NotificationSettings;
}
export type WatchlistAddresses = Array<WatchlistAddress>
import type { NextApiRequest, NextApiResponse } from 'next'
import fetch from './fetch';
type Methods = 'GET' | 'POST' | 'DELETE';
export default function handler<TRes>(getUrl: (_req: NextApiRequest) => string, allowedMethods: Array<Methods>) {
return async(_req: NextApiRequest, res: NextApiResponse<TRes>) => {
if (_req.method === 'GET' && allowedMethods.includes('GET')) {
const response = await fetch(getUrl(_req))
const data = await response.json() as TRes;
res.status(200).json(data);
} else if (allowedMethods.includes('POST') && _req.method === 'POST') {
const response = await fetch(getUrl(_req), {
method: 'POST',
body: _req.body,
})
const data = await response.json() as TRes;
res.status(200).json(data)
} else if (allowedMethods.includes('DELETE') && _req.method === 'DELETE') {
const response = await fetch(getUrl(_req), { method: 'DELETE' });
// FIXME: add error handlers
if (response.status !== 200) {
// eslint-disable-next-line no-console
console.log(response.statusText);
}
res.status(200).end();
} else {
res.setHeader('Allow', allowedMethods)
res.status(405).end(`Method ${ _req.method } Not Allowed`)
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment