Formatted all .ts files

This commit is contained in:
Donkie
2023-10-18 19:21:57 +02:00
parent 42cd250a92
commit f064ea589c
7 changed files with 321 additions and 332 deletions

View File

@@ -8,11 +8,8 @@ type MethodTypesWithBody = "post" | "put" | "patch";
const dataProvider = (
apiUrl: string,
httpClient: AxiosInstance = axiosInstance,
): Omit<
Required<DataProvider>,
"createMany" | "updateMany" | "deleteMany"
> => ({
httpClient: AxiosInstance = axiosInstance
): Omit<Required<DataProvider>, "createMany" | "updateMany" | "deleteMany"> => ({
getList: async ({ resource, meta, pagination, sorters, filters }) => {
const url = `${apiUrl}/${resource}`;
@@ -28,40 +25,41 @@ const dataProvider = (
}
if (sorters && sorters.length > 0) {
queryParams["sort"] = sorters.map((sort) => {
const field = sort.field
queryParams["sort"] = sorters
.map((sort) => {
const field = sort.field;
return `${field}:${sort.order}`;
}).join(",")
})
.join(",");
}
if (filters && filters.length > 0) {
filters.forEach(filter => {
filters.forEach((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) {
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>") {
return ""
return "";
}
return value
}).join(",")
return value;
})
.join(",");
queryParams[field] = filterValue
queryParams[field] = filterValue;
}
});
}
const { data, headers } = await httpClient[requestMethod](
`${url}`,
{
const { data, headers } = await httpClient[requestMethod](`${url}`, {
headers: headersFromMeta,
params: queryParams,
},
);
});
// console.log(url, requestMethod, queryParams, data, headers)
@@ -138,13 +136,7 @@ const dataProvider = (
return apiUrl;
},
custom: async ({
url,
method,
payload,
query,
headers,
}) => {
custom: async ({ url, method, payload, query, headers }) => {
let requestUrl = `${url}?`;
if (query) {

View File

@@ -28,17 +28,16 @@ function toWebsocketURL(apiUrl: string) {
const currentURL = window.location.href;
// Split the URL to separate the protocol, host, and path
const urlParts = currentURL.split('/');
const urlParts = currentURL.split("/");
const host = urlParts[2];
// Create the WebSocket URL by adding "ws://" as the protocol and the relative URL as the path
return `ws://${host}${apiUrl}`;
}
else {
} else {
// Absolute URL, e.g. "https://example.com/api/v1/..."
// Replace the protocol with "ws://"
return apiUrl.replace(/^http/, 'ws');
return apiUrl.replace(/^http/, "ws");
}
}
@@ -51,9 +50,15 @@ function toWebsocketURL(apiUrl: string) {
* @param id Specific ID to subscribe to, if any. If not specified, subscribes to all IDs.
* @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
if (!('WebSocket' in window)) {
if (!("WebSocket" in window)) {
console.warn("WebSockets are not supported in this browser. Live updates will not be available.");
return () => {};
}
@@ -74,7 +79,7 @@ function subscribeSingle(apiUrl: string, channel: string, resource: string, call
ids: [data.payload.id],
},
date: date,
}
};
callback(liveEvent);
};
@@ -84,27 +89,16 @@ function subscribeSingle(apiUrl: string, channel: string, resource: string, call
};
}
const liveProvider = (
apiUrl: string,
): LiveProvider => ({
const liveProvider = (apiUrl: string): LiveProvider => ({
subscribe: ({ channel, params, callback }) => {
const {
resource,
subscriptionType,
id,
ids,
} = params ?? {};
const { resource, subscriptionType, id, ids } = params ?? {};
if (!subscriptionType) {
throw new Error(
"[useSubscription]: `subscriptionType` is required in `params`",
);
throw new Error("[useSubscription]: `subscriptionType` is required in `params`");
}
if (!resource) {
throw new Error(
"[useSubscription]: `resource` is required in `params`",
);
throw new Error("[useSubscription]: `resource` is required in `params`");
}
let idList: BaseKey[];
@@ -112,7 +106,7 @@ const liveProvider = (
else if (id) idList = [id];
else {
// No ID specified, subscribe to all IDs
return [subscribeSingle(apiUrl, channel, resource, callback)]
return [subscribeSingle(apiUrl, channel, resource, callback)];
}
return idList.map((id) => {
@@ -122,6 +116,6 @@ const liveProvider = (
unsubscribe: (closers: (() => void)[]) => {
closers.forEach((fn) => fn());
},
})
});
export default liveProvider;

View File

@@ -1,6 +1,5 @@
import { ISpool } from "./model";
export async function setSpoolArchived(spool: ISpool, archived: boolean) {
const apiEndpoint = import.meta.env.VITE_APIURL;
const init: RequestInit = {

View File

@@ -16,11 +16,14 @@ export function typeFilters<Obj>(filters: CrudFilter[]): TypedCrudFilter<Obj>[]
* @param field The field to get the filter values for.
* @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[] = [];
filters.forEach((filter) => {
if (filter.field === field) {
filterValues.push(...filter.value as string[]);
filterValues.push(...(filter.value as string[]));
}
});
return filterValues;

View File

@@ -1,7 +1,6 @@
import { CrudSort } from "@refinedev/core";
import { SortOrder } from "antd/es/table/interface";
interface TypedCrudSort<Obj> {
field: keyof Obj;
order: "asc" | "desc";
@@ -13,7 +12,10 @@ interface TypedCrudSort<Obj> {
* @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.
*/
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);
if (sorter) {
return sorter.order === "asc" ? "ascend" : "descend";

View File

@@ -1,8 +1,7 @@
function _isLocalStorageAvailable(): boolean {
try {
localStorage.setItem('test', 'test');
localStorage.removeItem('test');
localStorage.setItem("test", "test");
localStorage.removeItem("test");
return true;
} catch (e) {
return false;