Skip to content

Commit

Permalink
feat: bring grant response type into shared package (#1377)
Browse files Browse the repository at this point in the history
- Bring grant response type into shared package
  • Loading branch information
rico191013 authored Jun 14, 2024
1 parent 93ffac2 commit f7e242d
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 66 deletions.
13 changes: 7 additions & 6 deletions packages/wallet/backend/src/grant/controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { NextFunction, Request } from 'express'
import { RafikiAuthService } from '@/rafiki/auth/service'
import { GetGrantsQuery, Grant } from '@/rafiki/auth/generated/graphql'
import { Grant } from '@/rafiki/auth/generated/graphql'
import { validate } from '@/shared/validate'
import { grantResponseSchema } from '@/grant/validation'
import { GrantService } from '@/grant/service'
import { Controller, toSuccessResponse } from '@shared/backend'
import { GrantResponse, GrantsListResponse } from '@wallet/shared'

interface IGrantController {
list: Controller<Grant[]>
Expand All @@ -22,7 +23,7 @@ export class GrantController implements IGrantController {

list = async (
req: Request,
res: CustomResponse<Grant[]>,
res: CustomResponse<GrantResponse[]>,
next: NextFunction
) => {
try {
Expand All @@ -35,7 +36,7 @@ export class GrantController implements IGrantController {

listWithPagination = async (
req: Request,
res: CustomResponse<GetGrantsQuery>,
res: CustomResponse<GrantsListResponse>,
next: NextFunction
) => {
try {
Expand All @@ -61,7 +62,7 @@ export class GrantController implements IGrantController {

getById = async (
req: Request,
res: CustomResponse<Grant>,
res: CustomResponse<GrantResponse>,
next: NextFunction
) => {
try {
Expand All @@ -75,7 +76,7 @@ export class GrantController implements IGrantController {

getByInteraction = async (
req: Request,
res: CustomResponse<Grant>,
res: CustomResponse<GrantResponse>,
next: NextFunction
) => {
try {
Expand All @@ -93,7 +94,7 @@ export class GrantController implements IGrantController {

setInteractionResponse = async (
req: Request,
res: CustomResponse<Grant>,
res: CustomResponse<GrantResponse>,
next: NextFunction
) => {
try {
Expand Down
4 changes: 2 additions & 2 deletions packages/wallet/frontend/src/components/GrantDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Grant } from '@/lib/api/grants'
import { Badge, getStatusBadgeIntent } from '@/ui/Badge'
import { SimpleArrow } from './icons/Arrow'
import { GrantResponse } from '@wallet/shared'

type GrantDetailsProps = { grant: Grant }
type GrantDetailsProps = { grant: GrantResponse }

export const GrantDetails = ({ grant }: GrantDetailsProps) => {
return (
Expand Down
51 changes: 4 additions & 47 deletions packages/wallet/frontend/src/lib/api/grants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,11 @@ import {
type ErrorResponse,
type SuccessResponse
} from '../httpClient'

const GRANT_STATE = {
APPROVED: 'APPROVED',
FINALIZED: 'FINALIZED',
PENDING: 'PENDING',
PROCESSING: 'PROCESSING'
} as const
type GrantState = keyof typeof GRANT_STATE

const GRANT_FINALIZATION = {
ISSUED: 'ISSUED',
REJECTED: 'REJECTED',
REVOKED: 'REVOKED'
}
type GrantFinalization = keyof typeof GRANT_FINALIZATION

type PaymentAmount = {
value: string
assetCode: string
assetScale: number
formattedAmount?: string
}

type Access = {
id: string
identifier: string | null
actions: string[]
type: string
limits: {
receiver: string | null
debitAmount: PaymentAmount | null
receiveAmount: PaymentAmount | null
interval: string | null
} | null
}

export type Grant = {
id: string
client: string
state: GrantState
createdAt: string
access: Access[]
finalizationReason?: GrantFinalization
}
import { GrantResponse } from '@wallet/shared'

type GrantNode = {
cursor: string
node: Grant
node: GrantResponse
}

export type GrantsPageInfo = {
Expand Down Expand Up @@ -80,10 +37,10 @@ export type GrantListArgs = z.infer<typeof grantsListSchema>
type GrantsListResult = SuccessResponse<GrantsList>
type GrantsListResponse = GrantsListResult | ErrorResponse

type ListGrantsResult = SuccessResponse<Grant[]>
type ListGrantsResult = SuccessResponse<GrantResponse[]>
type ListGrantsResponse = ListGrantsResult | ErrorResponse

type GetGrantResult = SuccessResponse<Grant>
type GetGrantResult = SuccessResponse<GrantResponse>
type GetGrantResponse = GetGrantResult | ErrorResponse
type DeleteGrantResponse = SuccessResponse | ErrorResponse

Expand Down
10 changes: 3 additions & 7 deletions packages/wallet/frontend/src/lib/hooks/useGrants.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { useHttpRequest } from './useHttp'
import { useCallback, useEffect, useState } from 'react'
import {
GrantListArgs,
GrantsList,
grantsListSchema,
grantsService
} from '../api/grants'
import { GrantListArgs, grantsListSchema, grantsService } from '../api/grants'
import { GRANTS_DISPLAY_NR } from '@/utils/constants'
import { useTypedRouter } from './useTypedRouter'
import { GrantsListResponse } from '@wallet/shared'

type GrantQueryParams = Record<keyof GrantListArgs, string | number>

Expand All @@ -27,7 +23,7 @@ export const useGrants = () => {
const router = useTypedRouter(grantsListSchema)
const { after, before, first, last } = router.query as GrantQueryParams
const [request, loading, error] = useHttpRequest()
const [grantsList, setGrantsList] = useState<GrantsList>(defaultState)
const [grantsList, setGrantsList] = useState<GrantsListResponse>(defaultState)

const fetch = useCallback(
async (pagination: Record<string, string | number>) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import type {
} from 'next/types'
import { z } from 'zod'
import { formatAmount, replaceWalletAddressProtocol } from '@/utils/helpers'
import { Grant, grantsService } from '@/lib/api/grants'
import { grantsService } from '@/lib/api/grants'
import { Button } from '@/ui/Button'
import Image from 'next/image'
import { useDialog } from '@/lib/hooks/useDialog'
import { ErrorDialog } from '@/components/dialogs/ErrorDialog'
import { useRouter } from 'next/router'
import { GrantResponse } from '@wallet/shared'

type GrantInteractionPageProps = InferGetServerSidePropsType<
typeof getServerSideProps
Expand Down Expand Up @@ -123,7 +124,7 @@ const querySchema = z.object({
})

export const getServerSideProps: GetServerSideProps<{
grant: Grant
grant: GrantResponse
interactionId: string
nonce: string
clientName: string
Expand Down
5 changes: 3 additions & 2 deletions packages/wallet/frontend/src/pages/grants/[grantId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
formatDate,
replaceWalletAddressProtocol
} from '@/utils/helpers'
import { Grant, grantsService } from '@/lib/api/grants'
import { grantsService } from '@/lib/api/grants'
import { Button } from '@/ui/Button'
import { useDialog } from '@/lib/hooks/useDialog'
import { ConfirmationDialog } from '@/components/dialogs/ConfirmationDialog'
Expand All @@ -20,6 +20,7 @@ import { ErrorDialog } from '@/components/dialogs/ErrorDialog'
import { useRouter } from 'next/router'
import Image from 'next/image'
import { GrantDetails } from '@/components/GrantDetails'
import { GrantResponse } from '@wallet/shared'

type GrantPageProps = InferGetServerSidePropsType<typeof getServerSideProps>

Expand Down Expand Up @@ -86,7 +87,7 @@ const querySchema = z.object({
})

export const getServerSideProps: GetServerSideProps<{
grant: Grant
grant: GrantResponse
}> = async (ctx) => {
const result = querySchema.safeParse(ctx.query)

Expand Down
61 changes: 61 additions & 0 deletions packages/wallet/shared/src/types/grant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const GRANT_STATE = {
APPROVED: 'APPROVED',
FINALIZED: 'FINALIZED',
PENDING: 'PENDING',
PROCESSING: 'PROCESSING'
} as const
type GrantState = keyof typeof GRANT_STATE

const GRANT_FINALIZATION = {
ISSUED: 'ISSUED',
REJECTED: 'REJECTED',
REVOKED: 'REVOKED'
}
type GrantFinalization = keyof typeof GRANT_FINALIZATION

type PaymentAmount = {
value: string
assetCode: string
assetScale: number
formattedAmount?: string
}

type Access = {
id: string
identifier: string | null
actions: string[]
type: string
limits: {
receiver: string | null
debitAmount: PaymentAmount | null
receiveAmount: PaymentAmount | null
interval: string | null
} | null
}

export interface GrantResponse {
id: string
client: string
state: GrantState
createdAt: string
access: Access[]
finalizationReason?: GrantFinalization
}

type GrantNode = {
cursor: string
node: GrantResponse
}

type GrantsPageInfo = {
endCursor: string
startCursor: string
hasNextPage: boolean
hasPreviousPage: boolean
}
export interface GrantsListResponse {
grants: {
edges: GrantNode[]
pageInfo: GrantsPageInfo
}
}
1 change: 1 addition & 0 deletions packages/wallet/shared/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './account'
export * from './asset'
export * from './rate'
export * from './grant'

0 comments on commit f7e242d

Please sign in to comment.