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: Replace 'data' for 'result' in API response #1063

Merged
merged 5 commits into from
Jan 16, 2024
Merged
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
2 changes: 1 addition & 1 deletion packages/boutique/backend/src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ declare global {
interface TypedResponseBody<T = any> {
success: boolean
message: string
data?: T
result?: T
errors?: Record<string, string>
}

Expand Down
12 changes: 6 additions & 6 deletions packages/boutique/backend/src/order/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NextFunction, Request } from 'express'
import { IOrderService } from './service'
import { Order } from './model'
import { BadRequest, InternalServerError } from '@/errors'
import { toSuccessReponse } from '@/shared/utils'
import { toSuccessResponse } from '@/shared/utils'
import { Logger } from 'winston'
import { IOpenPayments, TokenInfo } from '@/open-payments/service'
import { validate } from '@/middleware/validate'
Expand Down Expand Up @@ -57,7 +57,7 @@ export class OrderController implements IOrderController {

const order = await this.orderService.get(params.id)

res.status(200).json(toSuccessReponse(order))
res.status(200).json(toSuccessResponse(order))
} catch (err) {
this.logger.error(err)
next(err)
Expand Down Expand Up @@ -91,7 +91,7 @@ export class OrderController implements IOrderController {
this.logger.debug(JSON.stringify(grant, null, 2))
res
.status(201)
.json(toSuccessReponse({ redirectUrl: grant.interact.redirect }))
.json(toSuccessResponse({ redirectUrl: grant.interact.redirect }))
} catch (err) {
next(err)
}
Expand Down Expand Up @@ -153,7 +153,7 @@ export class OrderController implements IOrderController {
amount
)

res.status(200).json(toSuccessReponse({ redirectUrl }))
res.status(200).json(toSuccessResponse({ redirectUrl }))
} catch (err) {
next(err)
}
Expand Down Expand Up @@ -202,7 +202,7 @@ export class OrderController implements IOrderController {
})

res.status(200).json(
toSuccessReponse({
toSuccessResponse({
...tokenInfo,
walletAddressUrl: identifierData.walletAddressUrl
})
Expand Down Expand Up @@ -230,7 +230,7 @@ export class OrderController implements IOrderController {
const tokenInfo = await this.openPayments.instantBuy({ order, ...args })

res.status(200).json(
toSuccessReponse({
toSuccessResponse({
...tokenInfo,
walletAddressUrl: args.walletAddressUrl
})
Expand Down
6 changes: 3 additions & 3 deletions packages/boutique/backend/src/product/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NextFunction, Request } from 'express'
import { IProductService } from './service'
import { Product } from './model'
import { BadRequest } from '@/errors'
import { toSuccessReponse } from '@/shared/utils'
import { toSuccessResponse } from '@/shared/utils'
import { Logger } from 'winston'

interface GetParams {
Expand Down Expand Up @@ -36,7 +36,7 @@ export class ProductController implements IProductController {

const product = await this.productService.getBySlug(params.slug)

res.status(200).json(toSuccessReponse(product))
res.status(200).json(toSuccessResponse(product))
} catch (err) {
next(err)
}
Expand All @@ -49,7 +49,7 @@ export class ProductController implements IProductController {
) {
try {
const products = await this.productService.list()
res.status(200).json(toSuccessReponse(products))
res.status(200).json(toSuccessResponse(products))
} catch (err) {
this.logger.error(err)
next(err)
Expand Down
6 changes: 3 additions & 3 deletions packages/boutique/backend/src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ export function deleteProperty<T, K extends keyof T>(
interface SuccessResponse extends Omit<TypedResponseBody, 'errors'> {}
interface ErrorResponse extends Omit<TypedResponseBody, 'data'> {}

export function toSuccessReponse<T>(
data: T,
export function toSuccessResponse<T>(
result: T,
message: string = 'SUCCESS'
): SuccessResponse {
return {
success: true,
message: message,
data
result
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/boutique/backend/tests/product/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ describe('Application', (): void => {
expect(res._getJSONData()).toMatchObject({
success: true
})
expect(res._getJSONData().data.length).toBe(6)
expect(res._getJSONData().result.length).toBe(6)
})

it('should return an empty array if there are no products', async (): Promise<void> => {
await productController.list(req, res, next)
expect(res.statusCode).toBe(200)
expect(res._getJSONData()).toMatchObject({
success: true,
data: []
result: []
})
})

Expand Down Expand Up @@ -136,7 +136,7 @@ describe('Application', (): void => {
expect(res._getJSONData()).toMatchObject({
success: true,
message: 'SUCCESS',
data: product
result: product
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export const OneClickSetupDialog = ({
}
})

if (data?.data.redirectUrl) {
window.location.href = data.data.redirectUrl
if (data?.result.redirectUrl) {
window.location.href = data.result.redirectUrl
}

return (
Expand Down
4 changes: 2 additions & 2 deletions packages/boutique/frontend/src/app/cart/finish.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export function Component() {
result
},
{
onSuccess({ data }) {
setToken(data)
onSuccess({ result }) {
setToken(result)
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ const OpenPaymentsForm = () => {
}
})

if (data?.data.redirectUrl) {
if (data?.result.redirectUrl) {
resetCart()
window.location.href = data.data.redirectUrl
window.location.href = data.result.redirectUrl
}

return (
Expand Down
12 changes: 6 additions & 6 deletions packages/boutique/frontend/src/app/products/$slug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,29 @@ export function Component() {

if (data) {
return (
<ProductContext.Provider value={{ product: data.data }}>
<ProductContext.Provider value={{ product: data.result }}>
<div className="lg:grid lg:grid-cols-3 lg:gap-x-12">
<div className="aspect-h-1 aspect-w-1 mx-auto h-80 w-80 overflow-hidden rounded-md bg-gray-50 lg:w-full">
<img
src={`${IMAGES_URL}${data.data.image}`}
alt={data.data.name}
src={`${IMAGES_URL}${data.result.image}`}
alt={data.result.name}
className="h-full w-full object-cover object-center"
/>
</div>

<div className="col-span-2 mt-10 px-4 sm:mt-16 sm:px-0 lg:mt-0">
<h1 className="text-3xl font-bold tracking-tight">
{data.data.name}
{data.result.name}
</h1>
<div className="mt-3">
<p className="text-3xl tracking-tight">
{formatPrice(data.data.price)}
{formatPrice(data.result.price)}
</p>
</div>
<div className="prose mt-6 max-w-full">
<blockquote
className="quote space-y-6"
dangerouslySetInnerHTML={{ __html: data.data.description }}
dangerouslySetInnerHTML={{ __html: data.result.description }}
/>
</div>
<ProductCTA />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export const ProductCTA = () => {
}
})

if (data?.data.accessToken) {
setToken(data.data)
if (data?.result.accessToken) {
setToken(data.result)
return (
<Navigate to="/checkout/confirmation?instantBuy=true" replace={true} />
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const ProductsList = () => {
if (products.data) {
return (
<ProductsListWrapper>
{products.data.data.map((product) => (
{products.data.result.map((product) => (
<ProductCard key={product.id} product={product} />
))}
</ProductsListWrapper>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { APIError } from '@/lib/fetcher.ts'
import { SuccessReponse } from '@/lib/types.ts'
import { SuccessResponse } from '@/lib/types.ts'
import { UseMutationOptions } from '@tanstack/react-query'
import { z } from 'zod'
import { useCustomMutation } from './use-custom-mutation.ts'
Expand All @@ -24,7 +24,7 @@ export const createOrderSchema = z.object({

export function useCreateOrderMutation(
options?: UseMutationOptions<
SuccessReponse<CreateOrderMutationResponse>,
SuccessResponse<CreateOrderMutationResponse>,
APIError<z.infer<typeof createOrderSchema>>,
CreateOrderMutationParams
>
Expand Down
6 changes: 3 additions & 3 deletions packages/boutique/frontend/src/hooks/use-custom-mutation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { APIError, fetcher } from '@/lib/fetcher.ts'
import { SuccessReponse } from '@/lib/types.ts'
import { SuccessResponse } from '@/lib/types.ts'
import {
UseMutationOptions,
UseMutationResult,
Expand All @@ -21,11 +21,11 @@ export function useCustomMutation<
>(
fetcherOptions: FetcherOptions,
options?: UseMutationOptions<
SuccessReponse<TData>,
SuccessResponse<TData>,
APIError<TSchema>,
TParams
>
): UseMutationResult<SuccessReponse<TData>, APIError<TSchema>, TParams> {
): UseMutationResult<SuccessResponse<TData>, APIError<TSchema>, TParams> {
const { method, endpoint } = fetcherOptions
return useMutation({
mutationFn: async function (params: TParams) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { APIError } from '@/lib/fetcher.ts'
import { SuccessReponse } from '@/lib/types.ts'
import { SuccessResponse } from '@/lib/types.ts'
import { UseMutationOptions } from '@tanstack/react-query'
import { z } from 'zod'
import { useCustomMutation } from './use-custom-mutation.ts'
Expand All @@ -15,7 +15,7 @@ export const finishCheckoutSchema = z.object({
export function useFinishCheckoutMutation(
orderId: string,
options?: UseMutationOptions<
SuccessReponse,
SuccessResponse,
APIError<z.infer<typeof finishCheckoutSchema>>,
FinishCheckoutMutationParams
>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { APIError } from '@/lib/fetcher.ts'
import { SuccessReponse } from '@/lib/types.ts'
import { SuccessResponse } from '@/lib/types.ts'
import { UseMutationOptions } from '@tanstack/react-query'
import { z } from 'zod'
import { useCustomMutation } from './use-custom-mutation.ts'
Expand All @@ -14,7 +14,7 @@ export const finishSetupSchema = finishCheckoutSchema.extend({

export function useFinishSetupMutation(
options?: UseMutationOptions<
SuccessReponse<TokenState>,
SuccessResponse<TokenState>,
APIError<z.infer<typeof finishSetupSchema>>,
FinishSetupMutationParams
>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { APIError } from '@/lib/fetcher.ts'
import { SuccessReponse } from '@/lib/types.ts'
import { SuccessResponse } from '@/lib/types.ts'
import { UseMutationOptions } from '@tanstack/react-query'
import { z } from 'zod'
import { useCustomMutation } from './use-custom-mutation.ts'
Expand Down Expand Up @@ -32,7 +32,7 @@ export const instantBuySchema = z.object({

export function useInstantBuyMutation(
options?: UseMutationOptions<
SuccessReponse<InstantBuyMutationResponse>,
SuccessResponse<InstantBuyMutationResponse>,
APIError<z.infer<typeof instantBuySchema>>,
InstantBuyMutationParams
>
Expand Down
4 changes: 2 additions & 2 deletions packages/boutique/frontend/src/hooks/use-product-query.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { UseQueryResult, useQuery } from '@tanstack/react-query'
import { fetcher, APIError } from '@/lib/fetcher.ts'
import { SuccessReponse } from '@/lib/types.ts'
import { SuccessResponse } from '@/lib/types.ts'
import { Product } from './use-products-query.ts'
import { useZodRouteParams } from './use-zod-params.ts'
import { productSlugParamsSchema } from '@/app/route-schemas.ts'

export function useProductQuery(): UseQueryResult<
SuccessReponse<Product>,
SuccessResponse<Product>,
APIError
> {
const { slug } = useZodRouteParams(productSlugParamsSchema)
Expand Down
4 changes: 2 additions & 2 deletions packages/boutique/frontend/src/hooks/use-products-query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UseQueryResult, useQuery } from '@tanstack/react-query'
import { fetcher, APIError } from '@/lib/fetcher.ts'
import { SuccessReponse } from '@/lib/types.ts'
import { SuccessResponse } from '@/lib/types.ts'

export interface Product {
id: string
Expand All @@ -12,7 +12,7 @@ export interface Product {
}

export function useProductsQuery(): UseQueryResult<
SuccessReponse<Product[]>,
SuccessResponse<Product[]>,
APIError
> {
return useQuery({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { APIError } from '@/lib/fetcher.ts'
import { SuccessReponse } from '@/lib/types.ts'
import { SuccessResponse } from '@/lib/types.ts'
import { UseMutationOptions } from '@tanstack/react-query'
import { z } from 'zod'
import { useCustomMutation } from './use-custom-mutation.ts'
Expand All @@ -20,7 +20,7 @@ export const oneClickBuySetupSchema = z.object({

export function useSetupOneClickMutation(
options?: UseMutationOptions<
SuccessReponse<SetupOneClickMutationResponse>,
SuccessResponse<SetupOneClickMutationResponse>,
APIError<z.infer<typeof oneClickBuySetupSchema>>,
SetupOneClickMutationParams
>
Expand Down
4 changes: 2 additions & 2 deletions packages/boutique/frontend/src/lib/fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FieldValues, FieldPath } from 'react-hook-form'
import { API_BASE_URL } from './constants.ts'
import { ErrorResponse, SuccessReponse } from './types.ts'
import { ErrorResponse, SuccessResponse } from './types.ts'

export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'

Expand All @@ -22,7 +22,7 @@ export class APIError<T = undefined> extends Error {
export async function fetcher<JSON = any>(
input: string,
options?: RequestInit
): Promise<SuccessReponse<JSON>> {
): Promise<SuccessResponse<JSON>> {
const response = await fetch(API_BASE_URL + input, {
credentials: 'include',
headers: {
Expand Down
4 changes: 2 additions & 2 deletions packages/boutique/frontend/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export interface ErrorResponse {
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface SuccessReponse<T = any> {
export interface SuccessResponse<T = any> {
success: true
message: string
data: T
result: T
}
Loading