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

feat: connected to backend api #66

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
NEXT_PUBLIC_CODE_HASH_LIST=0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8
NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID=9200911ea59e455fe56bd123a15ff7d2
NEXT_PUBLIC_NETWORK=testnet
NEXT_PUBLIC_MOCK_API=true
NEXT_PUBLIC_MOCK_API=false
NEXT_PUBLIC_API_ENDPOINT=http://127.0.0.1:3000
NEXT_PUBLIC_CKB_RPC_URL=https://testnet.ckb.dev/rpc;
27 changes: 15 additions & 12 deletions app/components/apiClient/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ export function isAPIError(reason: unknown): reason is APIError {

export type FetchLike<R extends any = any> = (
input: string,
init?: {
method?: string
body?: string
headers: { [key in string]: string }
},
init?: NodeJS.fetch.RequestInit,
) => Promise<{
status: number
json(): Promise<R>
Expand All @@ -46,22 +42,29 @@ export class APIClient {
params: P = {} as P,
) {
return new Promise<R>((resolve, reject) => {
this.fetch(`${this.origin}/api/${endpoint}`, {
const init: NodeJS.fetch.RequestInit = {
method,
body: JSON.stringify({
...params,
}),
headers: {
'Content-Type': 'application/json',
},
})
}

if (Object.keys(params).length !== 0) {
Object.assign(init, {
body: JSON.stringify({
...params,
}),
})
}

this.fetch(`${this.origin}${endpoint}`, init)
.then(async (res) => {
const body = res.status === 204 ? null : await res.json()

if (res.status === 200) {
resolve(body)
resolve(body.data ?? body)
} else if (res.status === 204) {
resolve(body)
resolve(body.data ?? body)
} else {
reject({
[API_ERROR]: true,
Expand Down
36 changes: 21 additions & 15 deletions app/components/apiClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import {
Assets,
AddressHashParams,
TokenCreateData,
TokenUpdateData,
TokenTransferParams,
TokenMintParams,
Transaction,
ServerHistory,
ServerTransaction,
} from '@/app/type'
import { RawTransaction } from '@ckb-lumos/base'
import { APIClient, APIClientOptions } from './base'
import { MockApi } from './mock'

export class SUDTApi extends APIClient {
constructor(opts: APIClientOptions) {
Expand All @@ -21,26 +21,35 @@ export class SUDTApi extends APIClient {
this.get<Token[]>(`/token?${new URLSearchParams(params)}`),
detail: (args: string) => this.get<Token>(`/token/${args}`),
create: (data: TokenCreateData) =>
this.post<TokenCreateData, RawTransaction>('/token', data),
update: (data: TokenCreateData) =>
this.put<TokenCreateData, Token>('/token', data),
transfer: (data: TokenTransferParams) =>
this.post<TokenTransferParams, RawTransaction>('/token/transfer', data),
this.post<TokenCreateData, ServerTransaction>('/token', data),
update: (typeId: string, data: TokenUpdateData) =>
this.put<TokenUpdateData, Token>(`/token/${typeId}`, data),
transfer: (typeId: string, data: TokenTransferParams) =>
this.post<TokenTransferParams, ServerTransaction>(
`/token/send/${typeId}/`,
data,
),
mint: (typeId: string, params: TokenMintParams) =>
this.post<TokenMintParams, RawTransaction>(
this.post<TokenMintParams, ServerTransaction>(
`/token/mint/${typeId}/`,
params,
),
}

account = {
meta: (addressHash: string) =>
this.get<{ capacity: string }>(`/account/meta/${addressHash}`),
asyncAddress: (addressHash: string, addresses: string[]) =>
this.post(`/account/${addressHash}`, { addresses }),
listAssets: (addressHash: string) =>
this.get<Assets[]>(`/account/${addressHash}/assets`),

transferHistory: (addressHash: string) =>
this.get<Transaction[]>(
`/account/${addressHash}/assets/transfer/history`,
this.get<{
lastCursor: string
history: ServerHistory[]
}>(`/account/${addressHash}/assets/transaction`).then(
(res) => res.history,
),
}
}
Expand All @@ -51,7 +60,4 @@ const options = {
origin: process.env.NEXT_PUBLIC_API_ENDPOINT || '/',
}

export const sudtApi =
process.env.NEXT_PUBLIC_MOCK_API === 'true'
? new MockApi(options)
: new SUDTApi(options)
export const sudtApi = new SUDTApi(options)
37 changes: 0 additions & 37 deletions app/components/apiClient/mock.ts

This file was deleted.

Loading