Skip to content

Commit f593dd3

Browse files
committed
fetcher
1 parent 314fadc commit f593dd3

4 files changed

Lines changed: 65 additions & 56 deletions

File tree

src/vault/utility/create-fetch-helper.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1-
import { actionListFromResource, type ActionListFromResource } from './action-list-from-resource';
2-
import { resolveIfExists } from './resolve-if-exists';
3-
import { NormalizedEntity } from '../types';
41
import {
52
batchActions,
6-
requestError,
7-
requestResource,
83
RESOURCE_ERROR,
94
RESOURCE_LOADING,
105
RESOURCE_READY,
6+
requestError,
7+
requestResource,
118
} from '../actions';
9+
import type { NormalizedEntity } from '../types';
1210
import type { Vault } from '../vault';
11+
import { type ActionListFromResource, actionListFromResource } from './action-list-from-resource';
1312
import { isPromise } from './is-promise';
13+
import { resolveIfExists } from './resolve-if-exists';
1414

1515
export function createFetchHelper<T>(
1616
vault: Vault,
1717
fetcher: (url: string, options?: T) => any | Promise<any>,
1818
{
1919
waitTimeout = 30,
2020
actionListFromResource: actionListFromResourceFn = actionListFromResource,
21-
}: { waitTimeout?: number; actionListFromResource?: ActionListFromResource } = {}
21+
}: {
22+
waitTimeout?: number;
23+
actionListFromResource?: ActionListFromResource;
24+
} = {}
2225
) {
2326
return (
2427
url: string,
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
export const defaultFetcher = (url: string) => {
2-
return fetch(url).then((r) => {
3-
if (r.status === 200) {
4-
return r.json();
5-
}
6-
7-
const err = new Error(`${r.status} ${r.statusText}`);
8-
err.name = 'HTTPError';
9-
throw err;
10-
});
1+
export const defaultFetcher = async (url: string) => {
2+
const r = await fetch(url);
3+
if (r.status === 200) {
4+
return r.json();
5+
}
6+
const err = new Error(`${r.status} ${r.statusText}`);
7+
err.name = 'HTTPError';
8+
throw err;
119
};

src/vault/vault-auto.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type {
2222
RefToNormalized,
2323
RequestState,
2424
} from './types';
25+
import { defaultFetcher } from './utility/default-fetcher';
2526
import type { ReactiveWrapped } from './utility/objects';
2627
import { type GetObjectOptions, type GetOptions, Vault, type VaultOptions } from './vault';
2728
import { Vault4 } from './vault4';
@@ -58,7 +59,10 @@ function cloneForReplay<T>(value: T): T {
5859
return JSON.parse(JSON.stringify(value));
5960
}
6061

61-
function splitIdFragment(id: string): { idWithoutFragment: string; fragment?: string } {
62+
function splitIdFragment(id: string): {
63+
idWithoutFragment: string;
64+
fragment?: string;
65+
} {
6266
const hashIndex = id.indexOf('#');
6367
if (hashIndex === -1) {
6468
return { idWithoutFragment: id };
@@ -107,7 +111,7 @@ export class VaultAuto {
107111
enableDevtools,
108112
};
109113

110-
this.fetcher = customFetcher || this.defaultFetcher;
114+
this.fetcher = customFetcher || defaultFetcher;
111115
this.baseVault = new Vault(this.vaultOptions);
112116
this.activeVault = this.baseVault;
113117
}
@@ -128,17 +132,6 @@ export class VaultAuto {
128132
return this.activeVault;
129133
}
130134

131-
private defaultFetcher = (url: string) => {
132-
return fetch(url).then((r) => {
133-
if (r.status === 200) {
134-
return r.json();
135-
}
136-
const err = new Error(`${r.status} ${r.statusText}`);
137-
err.name = `HTTPError`;
138-
throw err;
139-
});
140-
};
141-
142135
private shouldUseAutoLoadPath(): boolean {
143136
return this.options.enablePresentation4 && !this.vault4Internal;
144137
}
@@ -362,7 +355,9 @@ export class VaultAuto {
362355
}
363356

364357
resolveAnnotationTargets(annotation: unknown): CanonicalSpecificResource[] {
365-
const fullAnnotation = this.get(annotation as any, { skipSelfReturn: false }) as any;
358+
const fullAnnotation = this.get(annotation as any, {
359+
skipSelfReturn: false,
360+
}) as any;
366361
if (!fullAnnotation || typeof fullAnnotation !== 'object') {
367362
return [];
368363
}
@@ -373,7 +368,9 @@ export class VaultAuto {
373368
}
374369

375370
resolveAnnotationBodies(annotation: unknown): any[] {
376-
const fullAnnotation = this.get(annotation as any, { skipSelfReturn: false }) as any;
371+
const fullAnnotation = this.get(annotation as any, {
372+
skipSelfReturn: false,
373+
}) as any;
377374
if (!fullAnnotation || typeof fullAnnotation !== 'object') {
378375
return [];
379376
}
@@ -383,7 +380,9 @@ export class VaultAuto {
383380
.filter((body) => body !== null && typeof body !== 'undefined')
384381
.map((body) => {
385382
if (body?.type === 'SpecificResource' && body?.source) {
386-
const unwrapped = this.get(body.source, { skipSelfReturn: false }) as any;
383+
const unwrapped = this.get(body.source, {
384+
skipSelfReturn: false,
385+
}) as any;
387386
return unwrapped ?? body.source;
388387
}
389388
return body;

src/vault/vault.ts

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
/// <reference types="geojson" />
22

33
import {
4+
frameResource,
5+
HAS_PART,
6+
isSpecificResource,
7+
PART_OF,
8+
type SerializeConfig,
9+
serialize,
10+
serializeConfigPresentation2,
11+
serializeConfigPresentation3,
12+
} from '@iiif/parser';
13+
import type { Collection, Manifest, Reference, SpecificResource } from '@iiif/parser/presentation-3/types';
14+
import type { CollectionNormalized, ManifestNormalized } from '@iiif/parser/presentation-3-normalized/types';
15+
import mitt, { type Emitter } from 'mitt';
16+
import { BATCH_ACTIONS, type BatchAction, batchActions, entityActions, metaActions } from './actions';
17+
import { createStore, type VaultZustandStore } from './store';
18+
import type {
419
ActionFromType,
520
AllActions,
621
Entities,
@@ -10,26 +25,11 @@ import {
1025
RefToNormalized,
1126
RequestState,
1227
} from './types';
13-
import { Collection, Manifest, Reference, SpecificResource } from '@iiif/parser/presentation-3/types';
14-
import {
15-
frameResource,
16-
HAS_PART,
17-
isSpecificResource,
18-
PART_OF,
19-
serialize,
20-
SerializeConfig,
21-
serializeConfigPresentation2,
22-
serializeConfigPresentation3,
23-
} from '@iiif/parser';
24-
import { BATCH_ACTIONS, BatchAction, batchActions, entityActions, metaActions } from './actions';
25-
import { createFetchHelper, areInputsEqual } from './utility';
26-
import { createStore, VaultZustandStore } from './store';
27-
import mitt, { Emitter } from 'mitt';
28-
import { CollectionNormalized, ManifestNormalized } from '@iiif/parser/presentation-3-normalized/types';
29-
import { isWrapped, ReactiveWrapped, wrapObject } from './utility/objects';
28+
import { areInputsEqual, createFetchHelper } from './utility';
29+
import { type ActionListFromResource, actionListFromResourceV3 } from './utility/action-list-from-resource';
30+
import { defaultFetcher } from './utility/default-fetcher';
31+
import { isWrapped, type ReactiveWrapped, wrapObject } from './utility/objects';
3032
import { resolveType } from './utility/resolve-type';
31-
import { actionListFromResourceV3, type ActionListFromResource } from './utility/action-list-from-resource';
32-
import { defaultFetcher as defaultVaultFetcher } from './utility/default-fetcher';
3333

3434
export type VaultOptions = {
3535
reducers: Record<string, any>;
@@ -67,7 +67,7 @@ export class Vault {
6767
this.options = Object.assign(
6868
{
6969
reducers: {},
70-
customFetcher: this.defaultFetcher,
70+
customFetcher: defaultFetcher,
7171
enableDevtools: true,
7272
},
7373
options || {}
@@ -92,7 +92,7 @@ export class Vault {
9292
return actionListFromResourceV3;
9393
}
9494

95-
defaultFetcher = defaultVaultFetcher;
95+
defaultFetcher = defaultFetcher;
9696

9797
batch(cb: (vault: this) => void) {
9898
this.isBatching = true;
@@ -141,12 +141,18 @@ export class Vault {
141141
if (!this.isBatching) {
142142
if (action.type === BATCH_ACTIONS) {
143143
for (const realAction of action.payload.actions) {
144-
this.emitter.emit(realAction.type, { action: realAction, state: this.store.getState() });
144+
this.emitter.emit(realAction.type, {
145+
action: realAction,
146+
state: this.store.getState(),
147+
});
145148
}
146149
this.store.dispatch(action);
147150
const state = this.getState();
148151
for (const realAction of action.payload.actions) {
149-
this.emitter.emit(`after:${realAction.type}`, { action: realAction, state });
152+
this.emitter.emit(`after:${realAction.type}`, {
153+
action: realAction,
154+
state,
155+
});
150156
}
151157
return;
152158
}
@@ -198,7 +204,10 @@ export class Vault {
198204
type?: string | GetOptions,
199205
options: GetOptions = {}
200206
): RefToNormalized<R> | RefToNormalized<R>[] {
201-
return this.get<R>(reference as any, type as any, { ...options, skipSelfReturn: false });
207+
return this.get<R>(reference as any, type as any, {
208+
...options,
209+
skipSelfReturn: false,
210+
});
202211
}
203212

204213
get<R extends { type?: string }>(

0 commit comments

Comments
 (0)