-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support document type on client hooks
- Loading branch information
1 parent
9bac238
commit 0a0427b
Showing
2 changed files
with
144 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,141 @@ | ||
import * as prismic from "@prismicio/client"; | ||
import * as prismicT from "@prismicio/types"; | ||
|
||
import { | ||
ClientHookReturnType, | ||
ClientMethodParameters, | ||
HookOnlyParameters, | ||
createClientHook, | ||
} from "./lib/createClientHook"; | ||
useStatefulPrismicClientMethod, | ||
} from "./useStatefulPrismicClientMethod"; | ||
|
||
const proto = prismic.Client.prototype; | ||
|
||
export const usePrismicDocuments = createClientHook< | ||
typeof proto.get, | ||
[params?: ClientMethodParameters<"get">[0] & HookOnlyParameters] | ||
>(proto.get); | ||
|
||
export const useFirstPrismicDocument = createClientHook< | ||
typeof proto.getFirst, | ||
[params?: ClientMethodParameters<"getFirst">[0] & HookOnlyParameters] | ||
>(proto.getFirst); | ||
|
||
export const useAllPrismicDocuments = createClientHook< | ||
typeof proto.getAll, | ||
[params?: ClientMethodParameters<"getAll">[0] & HookOnlyParameters] | ||
>(proto.getAll); | ||
|
||
export const usePrismicDocumentByID = createClientHook< | ||
typeof proto.getByID, | ||
[ | ||
export const usePrismicDocuments = <TDocument extends prismicT.PrismicDocument>( | ||
...args: [params?: ClientMethodParameters<"get">[0] & HookOnlyParameters] | ||
): ClientHookReturnType<prismic.Query<TDocument>> => | ||
useStatefulPrismicClientMethod(proto.get, args); | ||
|
||
export const useFirstPrismicDocument = < | ||
TDocument extends prismicT.PrismicDocument, | ||
>( | ||
...args: [params?: ClientMethodParameters<"getFirst">[0] & HookOnlyParameters] | ||
): ClientHookReturnType<TDocument> => | ||
useStatefulPrismicClientMethod(proto.getFirst, args); | ||
|
||
export const useAllPrismicDocuments = < | ||
TDocument extends prismicT.PrismicDocument, | ||
>( | ||
...args: [params?: ClientMethodParameters<"getAll">[0] & HookOnlyParameters] | ||
): ClientHookReturnType<TDocument[]> => | ||
useStatefulPrismicClientMethod(proto.getAll, args); | ||
|
||
export const usePrismicDocumentByID = < | ||
TDocument extends prismicT.PrismicDocument, | ||
>( | ||
...args: [ | ||
id: ClientMethodParameters<"getByID">[0], | ||
params?: ClientMethodParameters<"getByID">[1] & HookOnlyParameters, | ||
] | ||
>(proto.getByID); | ||
|
||
export const usePrismicDocumentsByIDs = createClientHook< | ||
typeof proto.getByIDs, | ||
[ | ||
ids: ClientMethodParameters<"getByIDs">[0], | ||
): ClientHookReturnType<TDocument> => | ||
useStatefulPrismicClientMethod(proto.getByID, args); | ||
|
||
export const usePrismicDocumentsByIDs = < | ||
TDocument extends prismicT.PrismicDocument, | ||
>( | ||
...args: [ | ||
id: ClientMethodParameters<"getByIDs">[0], | ||
params?: ClientMethodParameters<"getByIDs">[1] & HookOnlyParameters, | ||
] | ||
>(proto.getByIDs); | ||
|
||
export const useAllPrismicDocumentsByIDs = createClientHook< | ||
typeof proto.getAllByIDs, | ||
[ | ||
ids: ClientMethodParameters<"getAllByIDs">[0], | ||
): ClientHookReturnType<prismic.Query<TDocument>> => | ||
useStatefulPrismicClientMethod(proto.getByIDs, args); | ||
|
||
export const useAllPrismicDocumentsByIDs = < | ||
TDocument extends prismicT.PrismicDocument, | ||
>( | ||
...args: [ | ||
id: ClientMethodParameters<"getAllByIDs">[0], | ||
params?: ClientMethodParameters<"getAllByIDs">[1] & HookOnlyParameters, | ||
] | ||
>(proto.getAllByIDs); | ||
): ClientHookReturnType<TDocument[]> => | ||
useStatefulPrismicClientMethod(proto.getAllByIDs, args); | ||
|
||
export const usePrismicDocumentByUID = createClientHook< | ||
typeof proto.getByUID, | ||
[ | ||
export const usePrismicDocumentByUID = < | ||
TDocument extends prismicT.PrismicDocument, | ||
>( | ||
...args: [ | ||
documentType: ClientMethodParameters<"getByUID">[0], | ||
uid: ClientMethodParameters<"getByUID">[1], | ||
params?: ClientMethodParameters<"getByUID">[2] & HookOnlyParameters, | ||
] | ||
>(proto.getByUID); | ||
): ClientHookReturnType<TDocument> => | ||
useStatefulPrismicClientMethod(proto.getByUID, args); | ||
|
||
export const useSinglePrismicDocument = createClientHook< | ||
typeof proto.getSingle, | ||
[ | ||
export const useSinglePrismicDocument = < | ||
TDocument extends prismicT.PrismicDocument, | ||
>( | ||
...args: [ | ||
documentType: ClientMethodParameters<"getSingle">[0], | ||
params?: ClientMethodParameters<"getSingle">[1] & HookOnlyParameters, | ||
] | ||
>(proto.getSingle); | ||
): ClientHookReturnType<TDocument> => | ||
useStatefulPrismicClientMethod(proto.getSingle, args); | ||
|
||
export const usePrismicDocumentsByType = createClientHook< | ||
typeof proto.getByType, | ||
[ | ||
export const usePrismicDocumentsByType = < | ||
TDocument extends prismicT.PrismicDocument, | ||
>( | ||
...args: [ | ||
documentType: ClientMethodParameters<"getByType">[0], | ||
params?: ClientMethodParameters<"getByType">[1] & HookOnlyParameters, | ||
] | ||
>(proto.getByType); | ||
): ClientHookReturnType<prismic.Query<TDocument>> => | ||
useStatefulPrismicClientMethod(proto.getByType, args); | ||
|
||
export const useAllPrismicDocumentsByType = createClientHook< | ||
typeof proto.getAllByType, | ||
[ | ||
export const useAllPrismicDocumentsByType = < | ||
TDocument extends prismicT.PrismicDocument, | ||
>( | ||
...args: [ | ||
documentType: ClientMethodParameters<"getAllByType">[0], | ||
params?: ClientMethodParameters<"getAllByType">[1] & HookOnlyParameters, | ||
] | ||
>(proto.getAllByType); | ||
): ClientHookReturnType<TDocument[]> => | ||
useStatefulPrismicClientMethod(proto.getAllByType, args); | ||
|
||
export const usePrismicDocumentsByTag = createClientHook< | ||
typeof proto.getByTag, | ||
[ | ||
export const usePrismicDocumentsByTag = < | ||
TDocument extends prismicT.PrismicDocument, | ||
>( | ||
...args: [ | ||
tag: ClientMethodParameters<"getByTag">[0], | ||
params?: ClientMethodParameters<"getByTag">[1] & HookOnlyParameters, | ||
] | ||
>(proto.getByTag); | ||
): ClientHookReturnType<prismic.Query<TDocument>> => | ||
useStatefulPrismicClientMethod(proto.getByTag, args); | ||
|
||
export const useAllPrismicDocumentsByTag = createClientHook< | ||
typeof proto.getAllByTag, | ||
[ | ||
export const useAllPrismicDocumentsByTag = < | ||
TDocument extends prismicT.PrismicDocument, | ||
>( | ||
...args: [ | ||
tag: ClientMethodParameters<"getAllByTag">[0], | ||
params?: ClientMethodParameters<"getAllByTag">[1] & HookOnlyParameters, | ||
] | ||
>(proto.getAllByTag); | ||
): ClientHookReturnType<TDocument[]> => | ||
useStatefulPrismicClientMethod(proto.getAllByTag, args); | ||
|
||
export const usePrismicDocumentsByTags = createClientHook< | ||
typeof proto.getByTags, | ||
[ | ||
export const usePrismicDocumentsByTags = < | ||
TDocument extends prismicT.PrismicDocument, | ||
>( | ||
...args: [ | ||
tag: ClientMethodParameters<"getByTags">[0], | ||
params?: ClientMethodParameters<"getByTags">[1] & HookOnlyParameters, | ||
] | ||
>(proto.getByTags); | ||
): ClientHookReturnType<prismic.Query<TDocument>> => | ||
useStatefulPrismicClientMethod(proto.getByTags, args); | ||
|
||
export const useAllPrismicDocumentsByTags = createClientHook< | ||
typeof proto.getAllByTags, | ||
[ | ||
export const useAllPrismicDocumentsByTags = < | ||
TDocument extends prismicT.PrismicDocument, | ||
>( | ||
...args: [ | ||
tag: ClientMethodParameters<"getAllByTags">[0], | ||
params?: ClientMethodParameters<"getAllByTags">[1] & HookOnlyParameters, | ||
] | ||
>(proto.getAllByTags); | ||
): ClientHookReturnType<TDocument[]> => | ||
useStatefulPrismicClientMethod(proto.getAllByTags, args); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters