Skip to content

Endpoint queries

ts
class EndpointQuery<> {}

This is mobx-tanstack-query Query wrapper for Endpoint object.

Example:

ts
import { yourEndpoint } from "@/shared/api/__generated__";

export const yourEndpointQuery = yourEndpoint.toQuery({})

console.log(yourEndpointQuery.isLoading, yourEndpointQuery.data);

Differences between EndpointQuery and Query

params

This is the input params for EndpointQuery which are needed to enable or disable query.
Passing any falsy value in params property will disable the query, otherwise query will be enabled.

Examples with disabled queries:

ts
const query = getFruits.toQuery(() => ({
  params: null,
}));
const query = getFruits.toQuery(() => ({
  params: '',
}));
const query = getFruits.toQuery(() => ({
  params: false,
}));
const query = getFruits.toQuery(() => ({
  params: undefined,
}));
const query = getFruits.toQuery({
  params: 0,
});
const query = getFruits.toQuery({
  params: () => 0,
});

Examples with enabled queries:

ts
const query = getFruits.toQuery(() => ({
  params: {},
}));
const query = getFruits.toQuery(() => ({
  params: { query: {} },
}));
const query = getFruits.toQuery({
  params: { query: {} },
});
const query = getFruits.toQuery({
  params: () => ({ query: {} }),
});

If you will not write this property in options then query will be enabled only if query do not have any required params

ts
const query = getFruits.toQuery({});

// where getFruits
export const getFruits = new Endpoint<
  HttpResponse<..., ...>,
  ...,
  any
>(
  {
    params: ({ query, requestParams }) => ({
      path: `/api/v1/fruits`,
      method: "GET",
      query: query,
      ...requestParams,
    }),
    requiredParams: [],
    operationId: "getFruits",
    path: ["api", "v1", "services"],
    tags: [Tag.Fruits],
    meta: {} as any,
  },
  queryClient,
  httpClient,
);

update()

This method is also has params property which is needed to enable or disable query.

ts
const query = getFruits.toQuery({});

query.update({ params: {} });

Extras

ToEndpointQuery type

This type allows you to convert Endpoint to EndpointQuery type.
It might be helpful if you are using some factory method to create endpoint queries.

Example:

ts
import { ToEndpointQuery } from 'mobx-tanstack-query-api';
import { getFruits } from "@/shared/api/__generated__";

type GetFruitsQueryType = ToEndpointQuery<typeof getFruits>;

Released under the MIT License.