77 * file that was distributed with this source code.
88 */
99
10- import { type CookieClient } from '@adonisjs/core/http'
10+ import { type Router , type CookieClient } from '@adonisjs/core/http'
1111import { ApiClient , ApiRequest } from '@japa/api-client'
1212
1313import './types/extended.js'
1414import debug from './debug.ts'
15+ import { createURL } from '@adonisjs/core/helpers/http'
1516
1617/**
1718 * Extends the "@japa/api-client" plugin with AdonisJS-specific cookie methods.
@@ -21,15 +22,38 @@ import debug from './debug.ts'
2122 * cookies using the AdonisJS cookie client.
2223 *
2324 * @param cookieClient - The AdonisJS cookie client instance for cookie operations
25+ * @param router - The AdonisJS router instance for route building
2426 *
2527 * @example
2628 * ```js
27- * extendApiClient(app.container.use('cookie'))
29+ * extendApiClient(app.container.use('cookie'), app.container.use('router') )
2830 * ```
2931 */
30- export function extendApiClient ( cookieClient : CookieClient ) {
32+ export function extendApiClient ( cookieClient : CookieClient , router : Router ) {
3133 debug ( 'extending @japa/api-client with adonisjs specific methods' )
3234
35+ /**
36+ * Sets up route builder for generating URLs from named routes.
37+ *
38+ * This allows API client requests to use route names instead of hardcoded URLs.
39+ *
40+ * @param name - The route name to find
41+ * @param params - Route parameters for URL generation
42+ *
43+ * @example
44+ * ```js
45+ * client.get('users.show', { id: 1 })
46+ * // Resolves to: GET /users/1
47+ * ```
48+ */
49+ ApiClient . setRouteBuilder ( ( name , params ) => {
50+ const route = router . findOrFail ( name )
51+ return {
52+ url : createURL ( route . pattern , route . tokens , router . qs . stringify , params ) ,
53+ method : route . methods [ 0 ] ,
54+ }
55+ } )
56+
3357 /**
3458 * Cookie serializer for handling AdonisJS cookies in API responses.
3559 *
@@ -46,6 +70,12 @@ export function extendApiClient(cookieClient: CookieClient) {
4670 *
4771 * @param _ - Cookie key (unused)
4872 * @param value - Cookie value to prepare
73+ *
74+ * @example
75+ * ```js
76+ * prepare('session', 'signed-value')
77+ * // Returns: 'signed-value'
78+ * ```
4979 */
5080 prepare ( _ : string , value : any ) {
5181 return value
@@ -59,6 +89,12 @@ export function extendApiClient(cookieClient: CookieClient) {
5989 *
6090 * @param key - The cookie name
6191 * @param value - The raw cookie value from server response
92+ *
93+ * @example
94+ * ```js
95+ * process('session', 's:encrypted-value')
96+ * // Returns: original decrypted value
97+ * ```
6298 */
6399 process ( key : string , value : any ) {
64100 if ( ! value ) {
@@ -69,7 +105,18 @@ export function extendApiClient(cookieClient: CookieClient) {
69105 } )
70106
71107 /**
72- * Send a signed cookie during the API request
108+ * Sends a signed cookie during the API request.
109+ *
110+ * This macro method signs the cookie value using the AdonisJS cookie client
111+ * and adds it to the request's cookie jar.
112+ *
113+ * @param key - The cookie name
114+ * @param value - The cookie value to sign
115+ *
116+ * @example
117+ * ```js
118+ * client.get('/dashboard').withCookie('user_id', 123)
119+ * ```
73120 */
74121 ApiRequest . macro ( 'withCookie' , function ( this : ApiRequest , key : string , value : any ) {
75122 const signedValue = cookieClient . sign ( key , value )
@@ -79,12 +126,33 @@ export function extendApiClient(cookieClient: CookieClient) {
79126
80127 return this
81128 } )
129+
130+ /**
131+ * Deprecated alias for withCookie.
132+ *
133+ * @deprecated Use withCookie instead
134+ * @see withCookie
135+ *
136+ * @param key - The cookie name
137+ * @param value - The cookie value to sign
138+ */
82139 ApiRequest . macro ( 'cookie' , function ( this : ApiRequest , key : string , value : any ) {
83140 return this . withCookie ( key , value )
84141 } )
85142
86143 /**
87- * Send an encrypted cookie during the API request
144+ * Sends an encrypted cookie during the API request.
145+ *
146+ * This macro method encrypts the cookie value using the AdonisJS cookie client
147+ * and adds it to the request's cookie jar.
148+ *
149+ * @param key - The cookie name
150+ * @param value - The cookie value to encrypt
151+ *
152+ * @example
153+ * ```js
154+ * client.post('/login').withEncryptedCookie('session_data', { userId: 1 })
155+ * ```
88156 */
89157 ApiRequest . macro ( 'withEncryptedCookie' , function ( this : ApiRequest , key : string , value : any ) {
90158 const encryptedValue = cookieClient . encrypt ( key , value )
@@ -95,6 +163,15 @@ export function extendApiClient(cookieClient: CookieClient) {
95163 return this
96164 } )
97165
166+ /**
167+ * Deprecated alias for withEncryptedCookie.
168+ *
169+ * @deprecated Use withEncryptedCookie instead
170+ * @see withEncryptedCookie
171+ *
172+ * @param key - The cookie name
173+ * @param value - The cookie value to encrypt
174+ */
98175 ApiRequest . macro ( 'encryptedCookie' , function ( this : ApiRequest , key : string , value : any ) {
99176 return this . withEncryptedCookie ( key , value )
100177 } )
0 commit comments