Formatted all .ts files
This commit is contained in:
@@ -8,11 +8,8 @@ type MethodTypesWithBody = "post" | "put" | "patch";
|
|||||||
|
|
||||||
const dataProvider = (
|
const dataProvider = (
|
||||||
apiUrl: string,
|
apiUrl: string,
|
||||||
httpClient: AxiosInstance = axiosInstance,
|
httpClient: AxiosInstance = axiosInstance
|
||||||
): Omit<
|
): Omit<Required<DataProvider>, "createMany" | "updateMany" | "deleteMany"> => ({
|
||||||
Required<DataProvider>,
|
|
||||||
"createMany" | "updateMany" | "deleteMany"
|
|
||||||
> => ({
|
|
||||||
getList: async ({ resource, meta, pagination, sorters, filters }) => {
|
getList: async ({ resource, meta, pagination, sorters, filters }) => {
|
||||||
const url = `${apiUrl}/${resource}`;
|
const url = `${apiUrl}/${resource}`;
|
||||||
|
|
||||||
@@ -28,40 +25,41 @@ const dataProvider = (
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (sorters && sorters.length > 0) {
|
if (sorters && sorters.length > 0) {
|
||||||
queryParams["sort"] = sorters.map((sort) => {
|
queryParams["sort"] = sorters
|
||||||
const field = sort.field
|
.map((sort) => {
|
||||||
|
const field = sort.field;
|
||||||
return `${field}:${sort.order}`;
|
return `${field}:${sort.order}`;
|
||||||
}).join(",")
|
})
|
||||||
|
.join(",");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filters && filters.length > 0) {
|
if (filters && filters.length > 0) {
|
||||||
filters.forEach(filter => {
|
filters.forEach((filter) => {
|
||||||
if (!("field" in filter)) {
|
if (!("field" in filter)) {
|
||||||
throw Error("Filter must be a LogicalFilter.")
|
throw Error("Filter must be a LogicalFilter.");
|
||||||
}
|
}
|
||||||
const field = filter.field
|
const field = filter.field;
|
||||||
if (filter.value.length > 0) {
|
if (filter.value.length > 0) {
|
||||||
const filterValueArray = Array.isArray(filter.value) ? filter.value : [filter.value]
|
const filterValueArray = Array.isArray(filter.value) ? filter.value : [filter.value];
|
||||||
|
|
||||||
const filterValue = filterValueArray.map((value) => {
|
const filterValue = filterValueArray
|
||||||
|
.map((value) => {
|
||||||
if (value === "<empty>") {
|
if (value === "<empty>") {
|
||||||
return ""
|
return "";
|
||||||
}
|
}
|
||||||
return value
|
return value;
|
||||||
}).join(",")
|
})
|
||||||
|
.join(",");
|
||||||
|
|
||||||
queryParams[field] = filterValue
|
queryParams[field] = filterValue;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data, headers } = await httpClient[requestMethod](
|
const { data, headers } = await httpClient[requestMethod](`${url}`, {
|
||||||
`${url}`,
|
|
||||||
{
|
|
||||||
headers: headersFromMeta,
|
headers: headersFromMeta,
|
||||||
params: queryParams,
|
params: queryParams,
|
||||||
},
|
});
|
||||||
);
|
|
||||||
|
|
||||||
// console.log(url, requestMethod, queryParams, data, headers)
|
// console.log(url, requestMethod, queryParams, data, headers)
|
||||||
|
|
||||||
@@ -138,13 +136,7 @@ const dataProvider = (
|
|||||||
return apiUrl;
|
return apiUrl;
|
||||||
},
|
},
|
||||||
|
|
||||||
custom: async ({
|
custom: async ({ url, method, payload, query, headers }) => {
|
||||||
url,
|
|
||||||
method,
|
|
||||||
payload,
|
|
||||||
query,
|
|
||||||
headers,
|
|
||||||
}) => {
|
|
||||||
let requestUrl = `${url}?`;
|
let requestUrl = `${url}?`;
|
||||||
|
|
||||||
if (query) {
|
if (query) {
|
||||||
|
|||||||
@@ -28,17 +28,16 @@ function toWebsocketURL(apiUrl: string) {
|
|||||||
const currentURL = window.location.href;
|
const currentURL = window.location.href;
|
||||||
|
|
||||||
// Split the URL to separate the protocol, host, and path
|
// Split the URL to separate the protocol, host, and path
|
||||||
const urlParts = currentURL.split('/');
|
const urlParts = currentURL.split("/");
|
||||||
const host = urlParts[2];
|
const host = urlParts[2];
|
||||||
|
|
||||||
// Create the WebSocket URL by adding "ws://" as the protocol and the relative URL as the path
|
// Create the WebSocket URL by adding "ws://" as the protocol and the relative URL as the path
|
||||||
return `ws://${host}${apiUrl}`;
|
return `ws://${host}${apiUrl}`;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// Absolute URL, e.g. "https://example.com/api/v1/..."
|
// Absolute URL, e.g. "https://example.com/api/v1/..."
|
||||||
|
|
||||||
// Replace the protocol with "ws://"
|
// Replace the protocol with "ws://"
|
||||||
return apiUrl.replace(/^http/, 'ws');
|
return apiUrl.replace(/^http/, "ws");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,11 +50,17 @@ function toWebsocketURL(apiUrl: string) {
|
|||||||
* @param id Specific ID to subscribe to, if any. If not specified, subscribes to all IDs.
|
* @param id Specific ID to subscribe to, if any. If not specified, subscribes to all IDs.
|
||||||
* @returns A function to unsubscribe from the resource
|
* @returns A function to unsubscribe from the resource
|
||||||
*/
|
*/
|
||||||
function subscribeSingle(apiUrl: string, channel: string, resource: string, callback: (event: LiveEvent) => void, id?: BaseKey) {
|
function subscribeSingle(
|
||||||
|
apiUrl: string,
|
||||||
|
channel: string,
|
||||||
|
resource: string,
|
||||||
|
callback: (event: LiveEvent) => void,
|
||||||
|
id?: BaseKey
|
||||||
|
) {
|
||||||
// Verify that WebSockets are supported
|
// Verify that WebSockets are supported
|
||||||
if (!('WebSocket' in window)) {
|
if (!("WebSocket" in window)) {
|
||||||
console.warn("WebSockets are not supported in this browser. Live updates will not be available.");
|
console.warn("WebSockets are not supported in this browser. Live updates will not be available.");
|
||||||
return () => { };
|
return () => {};
|
||||||
}
|
}
|
||||||
|
|
||||||
const websocketURL = id ? toWebsocketURL(`${apiUrl}/${resource}/${id}`) : toWebsocketURL(`${apiUrl}/${resource}`);
|
const websocketURL = id ? toWebsocketURL(`${apiUrl}/${resource}/${id}`) : toWebsocketURL(`${apiUrl}/${resource}`);
|
||||||
@@ -74,7 +79,7 @@ function subscribeSingle(apiUrl: string, channel: string, resource: string, call
|
|||||||
ids: [data.payload.id],
|
ids: [data.payload.id],
|
||||||
},
|
},
|
||||||
date: date,
|
date: date,
|
||||||
}
|
};
|
||||||
|
|
||||||
callback(liveEvent);
|
callback(liveEvent);
|
||||||
};
|
};
|
||||||
@@ -84,27 +89,16 @@ function subscribeSingle(apiUrl: string, channel: string, resource: string, call
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const liveProvider = (
|
const liveProvider = (apiUrl: string): LiveProvider => ({
|
||||||
apiUrl: string,
|
|
||||||
): LiveProvider => ({
|
|
||||||
subscribe: ({ channel, params, callback }) => {
|
subscribe: ({ channel, params, callback }) => {
|
||||||
const {
|
const { resource, subscriptionType, id, ids } = params ?? {};
|
||||||
resource,
|
|
||||||
subscriptionType,
|
|
||||||
id,
|
|
||||||
ids,
|
|
||||||
} = params ?? {};
|
|
||||||
|
|
||||||
if (!subscriptionType) {
|
if (!subscriptionType) {
|
||||||
throw new Error(
|
throw new Error("[useSubscription]: `subscriptionType` is required in `params`");
|
||||||
"[useSubscription]: `subscriptionType` is required in `params`",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!resource) {
|
if (!resource) {
|
||||||
throw new Error(
|
throw new Error("[useSubscription]: `resource` is required in `params`");
|
||||||
"[useSubscription]: `resource` is required in `params`",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let idList: BaseKey[];
|
let idList: BaseKey[];
|
||||||
@@ -112,7 +106,7 @@ const liveProvider = (
|
|||||||
else if (id) idList = [id];
|
else if (id) idList = [id];
|
||||||
else {
|
else {
|
||||||
// No ID specified, subscribe to all IDs
|
// No ID specified, subscribe to all IDs
|
||||||
return [subscribeSingle(apiUrl, channel, resource, callback)]
|
return [subscribeSingle(apiUrl, channel, resource, callback)];
|
||||||
}
|
}
|
||||||
|
|
||||||
return idList.map((id) => {
|
return idList.map((id) => {
|
||||||
@@ -122,6 +116,6 @@ const liveProvider = (
|
|||||||
unsubscribe: (closers: (() => void)[]) => {
|
unsubscribe: (closers: (() => void)[]) => {
|
||||||
closers.forEach((fn) => fn());
|
closers.forEach((fn) => fn());
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
|
|
||||||
export default liveProvider;
|
export default liveProvider;
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { ISpool } from "./model";
|
import { ISpool } from "./model";
|
||||||
|
|
||||||
|
|
||||||
export async function setSpoolArchived(spool: ISpool, archived: boolean) {
|
export async function setSpoolArchived(spool: ISpool, archived: boolean) {
|
||||||
const apiEndpoint = import.meta.env.VITE_APIURL;
|
const apiEndpoint = import.meta.env.VITE_APIURL;
|
||||||
const init: RequestInit = {
|
const init: RequestInit = {
|
||||||
|
|||||||
@@ -16,11 +16,14 @@ export function typeFilters<Obj>(filters: CrudFilter[]): TypedCrudFilter<Obj>[]
|
|||||||
* @param field The field to get the filter values for.
|
* @param field The field to get the filter values for.
|
||||||
* @returns An array of filter values for the given field.
|
* @returns An array of filter values for the given field.
|
||||||
*/
|
*/
|
||||||
export function getFiltersForField<Obj, Field extends keyof Obj>(filters: TypedCrudFilter<Obj>[], field: Field): string[] {
|
export function getFiltersForField<Obj, Field extends keyof Obj>(
|
||||||
|
filters: TypedCrudFilter<Obj>[],
|
||||||
|
field: Field
|
||||||
|
): string[] {
|
||||||
const filterValues: string[] = [];
|
const filterValues: string[] = [];
|
||||||
filters.forEach((filter) => {
|
filters.forEach((filter) => {
|
||||||
if (filter.field === field) {
|
if (filter.field === field) {
|
||||||
filterValues.push(...filter.value as string[]);
|
filterValues.push(...(filter.value as string[]));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return filterValues;
|
return filterValues;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { CrudSort } from "@refinedev/core";
|
import { CrudSort } from "@refinedev/core";
|
||||||
import { SortOrder } from "antd/es/table/interface";
|
import { SortOrder } from "antd/es/table/interface";
|
||||||
|
|
||||||
|
|
||||||
interface TypedCrudSort<Obj> {
|
interface TypedCrudSort<Obj> {
|
||||||
field: keyof Obj;
|
field: keyof Obj;
|
||||||
order: "asc" | "desc";
|
order: "asc" | "desc";
|
||||||
@@ -13,7 +12,10 @@ interface TypedCrudSort<Obj> {
|
|||||||
* @param field The field to get the sort order for.
|
* @param field The field to get the sort order for.
|
||||||
* @returns The sort order for the given field, or undefined if the field is not being sorted.
|
* @returns The sort order for the given field, or undefined if the field is not being sorted.
|
||||||
*/
|
*/
|
||||||
export function getSortOrderForField<Obj, Field extends keyof Obj>(sorters: TypedCrudSort<Obj>[], field: Field): SortOrder | undefined {
|
export function getSortOrderForField<Obj, Field extends keyof Obj>(
|
||||||
|
sorters: TypedCrudSort<Obj>[],
|
||||||
|
field: Field
|
||||||
|
): SortOrder | undefined {
|
||||||
const sorter = sorters.find((s) => s.field === field);
|
const sorter = sorters.find((s) => s.field === field);
|
||||||
if (sorter) {
|
if (sorter) {
|
||||||
return sorter.order === "asc" ? "ascend" : "descend";
|
return sorter.order === "asc" ? "ascend" : "descend";
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
|
|
||||||
function _isLocalStorageAvailable(): boolean {
|
function _isLocalStorageAvailable(): boolean {
|
||||||
try {
|
try {
|
||||||
localStorage.setItem('test', 'test');
|
localStorage.setItem("test", "test");
|
||||||
localStorage.removeItem('test');
|
localStorage.removeItem("test");
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user