1- import { clientCredentialsHeader , passwordHeader } from "./authHandlers" ;
2- import type { Auth , Config , Options } from "./types" ;
1+ import { clientCredentialsHeader } from "./authHandlers" ;
2+ import type { Config , Options } from "./types" ;
33
4- const calculateAuthHeader = async < TAuth extends Auth [ "token_type" ] > (
4+ const calculateAuthHeader = async (
55 uri : string ,
6- type : TAuth ,
7- options : Options < TAuth > ,
6+ options : Options ,
87 fetcher : Config [ "fetcher" ]
98) => {
10- if ( type === "client_credentials" ) {
11- const { clientId, clientSecret} =
12- options as Options < "client_credentials" > ;
13- const header = await clientCredentialsHeader (
14- uri ,
15- clientId ,
16- clientSecret ,
17- fetcher ,
18- ) ;
19-
20- if ( header ) {
21- return header ;
22- }
23- }
24-
25- if ( type === "password" ) {
26- const { username, password, clientId, clientSecret } =
27- options as Options < "password" > ;
28- const header = await passwordHeader (
29- uri ,
30- username ,
31- password ,
32- clientId ,
33- clientSecret ,
34- fetcher ,
35- ) ;
9+ const { clientId, clientSecret } = options ;
10+ const header = await clientCredentialsHeader (
11+ uri ,
12+ clientId ,
13+ clientSecret ,
14+ fetcher
15+ ) ;
3616
37- if ( header ) {
38- return header ;
39- }
17+ if ( header ) {
18+ return header ;
4019 }
41-
42- return null ;
4320} ;
4421
4522/**
4623 * Return headers based on the given auth and config
4724 * @param uri The uri of the drupal site
48- * @param auth The auth strategy to use
4925 * @param options {Config} The auth options
5026 * @param config The config for the client
5127 *
5228 * The type for the options is decided by the auth type
5329 * @example
5430 * const client = drupalAuthClient(
5531 * "https://drupal.site",
56- * "client_credentials",
5732 * {
5833 * clientId: "client_id",
5934 * clientSecret: "client_secret",
6035 * },
6136 * );
62- *
63- * In the above example, you only need to provide the clientId and clientSecret
64- * because the auth type is "client_credentials" but if you set the auth type to
65- * "password" you would need to provide the username and password as well.
66- *
67- * @example
68- * const client = drupalAuthClient(
69- * "https://drupal.site",
70- * "password",
71- * {
72- * username: "username",
73- * password: "password",
74- * clientId: "client_id",
75- * clientSecret: "client_secret",
76- * }
77- * );
78- *
79- *
8037 **/
81- const drupalAuthClient = async < TAuth extends Auth [ "token_type" ] > (
38+ const drupalAuthClient = async (
8239 uri : string ,
83- type : TAuth ,
84- options : Options < TAuth > ,
40+ options : Options ,
8541 config : Config = {
8642 fetcher : fetch ,
8743 }
@@ -91,12 +47,7 @@ const drupalAuthClient = async <TAuth extends Auth["token_type"]>(
9147 const url = new URL ( uri ) ;
9248 const formattedAuthURI = authURI ? authURI : `${ url . origin } /oauth/token` ;
9349
94- const header = await calculateAuthHeader (
95- formattedAuthURI ,
96- type ,
97- options ,
98- fetcher
99- )
50+ const header = await calculateAuthHeader ( formattedAuthURI , options , fetcher ) ;
10051
10152 if ( ! header ) {
10253 throw new Error ( "Unable to fetch auth header" ) ;
@@ -106,8 +57,3 @@ const drupalAuthClient = async <TAuth extends Auth["token_type"]>(
10657} ;
10758
10859export { drupalAuthClient } ;
109-
110- /**
111- * @deprecated
112- */
113- export default drupalAuthClient ;
0 commit comments