Skip to content

Commit

Permalink
feat: check for matches
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed Jul 22, 2023
1 parent 90c17a4 commit 8c6e2f1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 42 deletions.
86 changes: 45 additions & 41 deletions packages/core/src/data/strategies/PostOrPostsFetchStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export class PostOrPostsFetchStrategy<
P extends PostOrPostsParams,
R extends PostOrPostsFetchStrategyResult<T>,
> extends AbstractFetchStrategy<T[], P, R> {
urlParams: Partial<P> = {};

postStrategy: SinglePostFetchStrategy = new SinglePostFetchStrategy(this.baseURL);

postsStrategy: PostsArchiveFetchStrategy = new PostsArchiveFetchStrategy(this.baseURL);
Expand All @@ -53,41 +55,42 @@ export class PostOrPostsFetchStrategy<
}

getParamsFromURL(path: string, params: Partial<P> = {}): Partial<P> {
console.log(
'PostsOrPosts - path',
path,
this.postStrategy.getParamsFromURL(path, params.single),
);
return {
this.urlParams = {
single: this.postStrategy.getParamsFromURL(path, params.single),
archive: this.postsStrategy.getParamsFromURL(path, params.archive),
} as P;

return this.urlParams;
}

async fetcher(
url: string,
params: Partial<P>,
options?: Partial<FetchOptions>,
): Promise<FetchResponse<R>> {
if (params.priority === 'single') {
try {
const results = await this.postStrategy.fetcher(
this.postStrategy.buildEndpointURL(params.single ?? {}),
params.single ?? {},
options,
);
const didMatchSingle = Object.keys(this.urlParams?.single ?? {}).length > 0;
const didMatchArchive = Object.keys(this.urlParams?.archive ?? {}).length > 0;

return {
...results,
result: {
isArchive: false,
isSingle: true,
data: results.result,
} as R,
};
} catch (e) {
console.log(e);
// do nothing
if (params.priority === 'single') {
if (didMatchSingle) {
try {
const results = await this.postStrategy.fetcher(
this.postStrategy.buildEndpointURL(params.single ?? {}),
params.single ?? {},
options,
);

return {
...results,
result: {
isArchive: false,
isSingle: true,
data: results.result,
} as R,
};
} catch (e) {
// do nothing
}
}

// TODO: capture potentiall error and throw a better error message
Expand All @@ -108,24 +111,25 @@ export class PostOrPostsFetchStrategy<
};
}

try {
const results = await this.postsStrategy.fetcher(
this.postsStrategy.buildEndpointURL(params.archive ?? {}),
params.archive ?? {},
options,
);
if (didMatchArchive) {
try {
const results = await this.postsStrategy.fetcher(
this.postsStrategy.buildEndpointURL(params.archive ?? {}),
params.archive ?? {},
options,
);

return {
...results,
result: {
isArchive: true,
isSingle: false,
data: results.result,
} as R,
};
} catch (e) {
console.log('archive not found', e);
// do nothing
return {
...results,
result: {
isArchive: true,
isSingle: false,
data: results.result,
} as R,
};
} catch (e) {
// do nothing
}
}

const results = await this.postStrategy.fetcher(
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/data/hooks/usePostOrPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export function usePostOrPosts<
) {
const useFetchArguments = usePrepareFetch(params, options);

console.log('usePostOrPosts', useFetchArguments);
return useFetchPostOrPosts<T, P>(
useFetchArguments.params,
useFetchArguments.options,
Expand Down

0 comments on commit 8c6e2f1

Please sign in to comment.