Home > Software engineering >  React Fetch Request, Remove outer type class name
React Fetch Request, Remove outer type class name

Time:08-08

I am passing data into a Fetch request with React. Its currently passing in object type name also (getPaymentRequest). How do I remove the outer type class? See below.

export const getPayments = (
  customerId: number,
  getPaymentsRequest: GetPaymentsRequest,
): Promise<PageSortedResponsePayment> => {
  return kfetch(`/billing-payments-ui/api/getPayments/${customerId}`, {
    method: 'put',
    body: JSON.stringify({
      getPaymentsRequest,
    }),
  });
};

export type GetPaymentsRequest = {
  pageNumber?: number;
  pageSize?: number;
  patientName?: string;
  paymentEndDate?: Date;
  paymentStartDate?: Date;
  paymentStatus?: string;
  sortField?: string;
};

Current:

{"getPaymentsRequest":{"pageNumber":2,"pageSize":10,"paymentEndDate":"2022-09-01T07:00:00.000Z","paymentStartDate":"2020-02-01T08:00:00.000Z"}}

Remove Outer class name:

{"pageNumber":2,"pageSize":10,"paymentEndDate":"2022-09-01T07:00:00.000Z","paymentStartDate":"2020-02-01T08:00:00.000Z"}

CodePudding user response:

You can do it with Object.keys().

const outerClass = Object.keys(getPayments)[0];
const result = getPayments[outerClass];

or if your outer always the same just

const result = getPayments[getPaymentsRequest];

CodePudding user response:

maybe you can try this:

body: JSON.stringify({
      ...getPaymentsRequest
    }),

if there is any better solution, please let me know

  • Related