Skip to content

test workflows #17

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

Closed
wants to merge 2 commits into from
Closed
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
219 changes: 115 additions & 104 deletions src/runtime/defineResource/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
import { useNuxtApp } from "nuxt/app";

import search from "../methods/search";
import mutate from "../methods/mutate";
import details from "../methods/details";
import actions from "../methods/actions";
import remove from "../methods/delete";
import forceDelete from "../methods/forceDelete";
import restore from "../methods/restore";
import { $fetch } from 'ofetch'

import { $fetch } from "ofetch";

import type { IMutateRequest, IMutateResponse } from "../types/mutate";
import type { ISearchQuery, ISearchResponse } from "../types/search";
import type { IActionRequest, IActionResponse } from "../types/actions";
import type { IResourcePreset } from "../types/resourceConfig";
import type { FetchOptions } from "../types/fetchOptions";


const combineHooks = (globalHook, specificHook) => {
return async (arg) => {
if (typeof globalHook === "function") await globalHook(arg);
if (typeof specificHook === "function") await specificHook(arg);
};
}

return async (arg) => {
if (typeof globalHook === "function") await globalHook(arg);
if (typeof specificHook === "function") await specificHook(arg);
};
};

const joinUrl = (base: string, path: string) =>
base.replace(/\/+$/, "") + "/" + path.replace(/^\/+/, "");

base.replace(/\/+$/, "") + "/" + path.replace(/^\/+/, "");

/**
* @author Eliott Depauw
Expand All @@ -41,97 +36,113 @@ const joinUrl = (base: string, path: string) =>
* }
* });
*/
const defineResource = <T>(resourceName: string, preset: IResourcePreset<T> = {}) => (additionalPreset: IResourcePreset<T> = {}) => {

// @ts-ignore
const globalFetchOptions = useNuxtApp().$restApiSdk.getGlobalFetchOptions() as FetchOptions | null;

if (!resourceName)
throw new Error("Resource name is required");

const presets = { ...preset, ...additionalPreset };

const resourceUrl = joinUrl(globalFetchOptions?.baseURL ?? "", `/${resourceName}`);

// @ts-ignore
const api = $fetch.create({
...globalFetchOptions,
baseURL: resourceUrl,
onRequest: combineHooks(globalFetchOptions?.onRequest, presets.onRequest),
onRequestError: combineHooks(globalFetchOptions?.onRequestError, presets.onRequestError),
onResponse: combineHooks(globalFetchOptions?.onResponse, presets.onResponse),
onResponseError: combineHooks(globalFetchOptions?.onResponseError, presets.onResponseError),
})


return {
/**
* @description Fetches the details of a resource.
* @returns {Promise<any>} The details of the resource.
*/
details: (): Promise<any> => details(api),

/**
* @description Searches for resources.
* @param {ISearchQuery<T>} [request={}] The search request parameters.
* @returns {Promise<ISearchResponse<T>>} An array of resources matching the search criteria.
* @example
* const { data, total } = await productsResource.search({
* filters: [{ field: "category.name", value: "electronics" }]
* });
*/
search: (request: ISearchQuery<T> = {}): Promise<ISearchResponse<T>> => search({ ...presets.search, ...request }, api),

/**
* @description Mutates resources.
* @param {IMutateRequest<T>[]} mutations The mutations to apply to the resources.
* @returns {Promise<IMutateResponse>} The result of the mutation operation.
* @example
* const response = await productsResource.mutate([
* {
* operation: "update",
* key: 2,
* attributes: { "price": 19.99 },
* },
* ])
*/
mutate: (mutations: IMutateRequest<T>[]): Promise<IMutateResponse> => mutate(mutations, api),
/**
* @description Executes an action on a resource.
* @param {string} actionName The name of the action to execute.
* @param {object} params The parameters for the action.
* @returns {Promise<IActionResponse>} The result of the action.
* @example
* const response = await cartsResource.actions("validate-cart", {
* search: {
* filters: [{ field: "id", value: 1 }],
* },
* });
*/
actions: (actionName: string, params: IActionRequest<T>): Promise<IActionResponse> => actions(actionName, params, api),

/**
* @description Removes resources by their IDs.
* @param {number[]} ids The IDs of the resources to remove.
* @returns {Promise<any>} The result of the remove operation.
*/
remove: (ids: number[]): Promise<any> => remove(ids, api),

/**
* @description Force delete resources by their IDs.
* @param {number[]} ids The IDs of the resources to force delete.
* @returns {Promise<any>} The result of the force delete operation.
*/
forceDelete: (ids: number[]): Promise<any> => forceDelete(ids, api),


/**
* @description Force delete resources by their IDs.
* @param {number[]} ids The IDs of the resources to force delete.
* @returns {Promise<any>} The result of the force delete operation.
*/
restore: (ids: number[]): Promise<any> => restore(ids, api),
};
};
const defineResource =
<T>(resourceName: string, preset: IResourcePreset<T> = {}) =>
(additionalPreset: IResourcePreset<T> = {}) => {
// @ts-ignore
const globalFetchOptions =
useNuxtApp().$restApiSdk.getGlobalFetchOptions() as FetchOptions | null;

if (!resourceName) throw new Error("Resource name is required");

const presets = { ...preset, ...additionalPreset };

const resourceUrl = joinUrl(
globalFetchOptions?.baseURL ?? "",
`/${resourceName}`
);

// @ts-ignore
const api = $fetch.create({
...globalFetchOptions,
baseURL: resourceUrl,
onRequest: combineHooks(globalFetchOptions?.onRequest, presets.onRequest),
onRequestError: combineHooks(
globalFetchOptions?.onRequestError,
presets.onRequestError
),
onResponse: combineHooks(
globalFetchOptions?.onResponse,
presets.onResponse
),
onResponseError: combineHooks(
globalFetchOptions?.onResponseError,
presets.onResponseError
),
});

return {
/**
* @description Fetches the details of a resource.
* @returns {Promise<any>} The details of the resource.
*/
details: (): Promise<any> => details(api),

/**
* @description Searches for resources.
* @param {ISearchQuery<T>} [request={}] The search request parameters.
* @returns {Promise<ISearchResponse<T>>} An array of resources matching the search criteria.
* @example
* const { data, total } = await productsResource.search({
* filters: [{ field: "category.name", value: "electronics" }]
* });
*/
search: (request: ISearchQuery<T> = {}): Promise<ISearchResponse<T>> =>
search({ ...presets.search, ...request }, api),

/**
* @description Mutates resources.
* @param {IMutateRequest<T>[]} mutations The mutations to apply to the resources.
* @returns {Promise<IMutateResponse>} The result of the mutation operation.
* @example
* const response = await productsResource.mutate([
* {
* operation: "update",
* key: 2,
* attributes: { "price": 19.99 },
* },
* ])
*/
mutate: (mutations: IMutateRequest<T>[]): Promise<IMutateResponse> =>
mutate(mutations, api),
/**
* @description Executes an action on a resource.
* @param {string} actionName The name of the action to execute.
* @param {object} params The parameters for the action.
* @returns {Promise<IActionResponse>} The result of the action.
* @example
* const response = await cartsResource.actions("validate-cart", {
* search: {
* filters: [{ field: "id", value: 1 }],
* },
* });
*/
actions: (
actionName: string,
params: IActionRequest<T>
): Promise<IActionResponse> => actions(actionName, params, api),

/**
* @description Removes resources by their IDs.
* @param {number[]} ids The IDs of the resources to remove.
* @returns {Promise<any>} The result of the remove operation.
*/
remove: (ids: number[]): Promise<any> => remove(ids, api),

/**
* @description Force delete resources by their IDs.
* @param {number[]} ids The IDs of the resources to force delete.
* @returns {Promise<any>} The result of the force delete operation.
*/
forceDelete: (ids: number[]): Promise<any> => forceDelete(ids, api),

/**
* @description Force delete resources by their IDs.
* @param {number[]} ids The IDs of the resources to force delete.
* @returns {Promise<any>} The result of the force delete operation.
*/
restore: (ids: number[]): Promise<any> => restore(ids, api),
};
};

export default defineResource;
Loading