Fix fetch (missing request headers etc.)

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak
2024-05-23 18:16:10 +02:00
parent 8ea94e548b
commit 6daa4672d2
23 changed files with 79 additions and 40 deletions

View File

@ -1,4 +1,5 @@
import type { Entity } from '../types';
import type { PlfeResponse } from 'soapbox/api';
import type z from 'zod';
type EntitySchema<TEntity extends Entity = Entity> = z.ZodType<TEntity, z.ZodTypeDef, any>;
@ -32,9 +33,9 @@ interface EntityCallbacks<Value, Error = unknown> {
/**
* Passed into hooks to make requests.
* Must return an Axios response.
* Must return a response.
*/
type EntityFn<T> = (value: T) => Promise<Response>
type EntityFn<T> = (value: T) => Promise<PlfeResponse>
export type {
EntitySchema,

View File

@ -9,6 +9,7 @@ import { parseEntitiesPath } from './utils';
import type { EntityCallbacks, EntityFn, EntitySchema, ExpandedEntitiesPath } from './types';
import type { Entity } from '../types';
import type { PlfeResponse } from 'soapbox/api';
interface UseCreateEntityOpts<TEntity extends Entity = Entity> {
schema?: EntitySchema<TEntity>;
@ -26,7 +27,7 @@ const useCreateEntity = <TEntity extends Entity = Entity, Data = unknown>(
const createEntity = async (
data: Data,
callbacks: EntityCallbacks<TEntity, { response?: Response & { json: any } }> = {},
callbacks: EntityCallbacks<TEntity, { response?: PlfeResponse }> = {},
): Promise<void> => {
const result = await setPromise(entityFn(data));
const schema = opts.schema || z.custom<TEntity>();

View File

@ -10,6 +10,7 @@ import { selectEntity } from '../selectors';
import type { EntitySchema, EntityPath, EntityFn } from './types';
import type { Entity } from '../types';
import type { PlfeResponse } from 'soapbox/api';
/** Additional options for the hook. */
interface UseEntityOpts<TEntity extends Entity> {
@ -66,8 +67,8 @@ const useEntity = <TEntity extends Entity>(
isLoading,
isLoaded,
error,
isUnauthorized: (error as { response?: Response })?.response?.status === 401,
isForbidden: (error as { response?: Response })?.response?.status === 403,
isUnauthorized: (error as { response?: PlfeResponse })?.response?.status === 401,
isForbidden: (error as { response?: PlfeResponse })?.response?.status === 403,
};
};

View File

@ -7,10 +7,11 @@ import { useLoading } from 'soapbox/hooks/useLoading';
import { importEntities } from '../actions';
import { findEntity } from '../selectors';
import { Entity } from '../types';
import { EntityFn } from './types';
import { type UseEntityOpts } from './useEntity';
import type { EntityFn } from './types';
import type { UseEntityOpts } from './useEntity';
import type { Entity } from '../types';
import type { PlfeResponse } from 'soapbox/api';
/** Entities will be filtered through this function until it returns true. */
type LookupFn<TEntity extends Entity> = (entity: TEntity) => boolean
@ -56,8 +57,8 @@ const useEntityLookup = <TEntity extends Entity>(
fetchEntity,
isFetching,
isLoading,
isUnauthorized: (error as { response?: Response })?.response?.status === 401,
isForbidden: (error as { response?: Response })?.response?.status === 403,
isUnauthorized: (error as { response?: PlfeResponse })?.response?.status === 401,
isForbidden: (error as { response?: PlfeResponse })?.response?.status === 403,
};
};