Skip to content

Commit f684469

Browse files
committed
feat: add route builder to APIClient and add docblocks
1 parent 9dd5c8a commit f684469

File tree

5 files changed

+111
-8
lines changed

5 files changed

+111
-8
lines changed

index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,15 @@ export function pluginAdonisJS(app: ApplicationService, options?: { baseURL: str
4444
/**
4545
* Extend "@japa/api-client" plugin
4646
*/
47-
if ((await canImport('@japa/api-client')) && app.container.hasBinding('encryption')) {
47+
if (
48+
(await canImport('@japa/api-client')) &&
49+
app.container.hasAllBindings(['encryption', 'router'])
50+
) {
4851
const { extendApiClient } = await import('./src/extend_api_client.js')
49-
extendApiClient(new CookieClient(await app.container.make(Encryption)))
52+
extendApiClient(
53+
new CookieClient(await app.container.make(Encryption)),
54+
await app.container.make('router')
55+
)
5056
}
5157

5258
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"@adonisjs/eslint-config": "^3.0.0-next.5",
4040
"@adonisjs/prettier-config": "^1.4.5",
4141
"@adonisjs/tsconfig": "^2.0.0-next.3",
42-
"@japa/api-client": "^3.2.0",
42+
"@japa/api-client": "^3.2.1",
4343
"@japa/assert": "^4.2.0",
4444
"@japa/browser-client": "^2.3.0",
4545
"@japa/runner": "^5.0.0",

src/extend_api_client.ts

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
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'
1111
import { ApiClient, ApiRequest } from '@japa/api-client'
1212

1313
import './types/extended.js'
1414
import 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
})

src/extend_browser_client.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ import debug from './debug.ts'
2323
*
2424
* @param defaultDomain - The default domain to use for cookies
2525
* @param options - Optional cookie options to merge with defaults
26+
*
27+
* @example
28+
* ```js
29+
* normalizeCookieOptions('localhost', { httpOnly: true })
30+
* // Returns: { domain: 'localhost', path: '/', httpOnly: true }
31+
* ```
2632
*/
2733
function normalizeCookieOptions(defaultDomain?: string, options?: CookieOptions): CookieOptions {
2834
return Object.assign(
@@ -42,6 +48,15 @@ function normalizeCookieOptions(defaultDomain?: string, options?: CookieOptions)
4248
* if decoding fails.
4349
*
4450
* @param value - The cookie value to decode
51+
*
52+
* @example
53+
* ```js
54+
* tryDecode('hello%20world')
55+
* // Returns: 'hello world'
56+
*
57+
* tryDecode('invalid%')
58+
* // Returns: 'invalid%' (original value due to decode error)
59+
* ```
4560
*/
4661
function tryDecode(value: string) {
4762
try {

src/extend_context.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ export function extendContext(router: Router, repl: Repl) {
3939
* allowing for interactive debugging during test execution.
4040
*
4141
* @param context - Optional context object to make available in the REPL
42+
*
43+
* @example
44+
* ```js
45+
* await startRepl({ user: currentUser, app })
46+
* ```
4247
*/
4348
function startRepl(context?: Record<any, any>) {
4449
return new Promise<void>((resolve) => {

0 commit comments

Comments
 (0)