Skip to content

Commit a67d16c

Browse files
committed
feat: add form property to url_builder return type
The form property can be used to define the form attributes
1 parent 2ff1ec3 commit a67d16c

File tree

4 files changed

+134
-40
lines changed

4 files changed

+134
-40
lines changed

src/router/signed_url_builder.ts

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,61 +57,96 @@ export function createSignedUrlBuilder<Routes extends LookupList>(
5757
}
5858

5959
signedRoute.get = function routeGet(...[identifier, params, options]) {
60+
const url = createSignedUrlForRoute(identifier, params, options, 'GET')
61+
const method = 'get'
6062
return {
61-
url: createSignedUrlForRoute(identifier, params, options, 'GET'),
62-
method: 'get',
63+
url,
64+
method,
6365
toString() {
64-
return this.url
66+
return url
67+
},
68+
form: {
69+
action: url,
70+
method,
6571
},
6672
}
6773
}
6874

6975
signedRoute.post = function routePost(...[identifier, params, options]) {
76+
const url = createSignedUrlForRoute(identifier, params, options, 'POST')
77+
const method = 'post'
7078
return {
71-
url: createSignedUrlForRoute(identifier, params, options, 'POST'),
72-
method: 'post',
79+
url,
80+
method,
7381
toString() {
74-
return this.url
82+
return url
83+
},
84+
form: {
85+
action: url,
86+
method,
7587
},
7688
}
7789
}
7890

7991
signedRoute.put = function routePut(...[identifier, params, options]) {
92+
const url = createSignedUrlForRoute(identifier, params, options, 'PUT')
93+
const method = 'put'
8094
return {
81-
url: createSignedUrlForRoute(identifier, params, options, 'PUT'),
82-
method: 'put',
95+
url,
96+
method,
8397
toString() {
84-
return this.url
98+
return url
99+
},
100+
form: {
101+
action: url,
102+
method,
85103
},
86104
}
87105
}
88106

89107
signedRoute.patch = function routePatch(...[identifier, params, options]) {
108+
const url = createSignedUrlForRoute(identifier, params, options, 'PATCH')
109+
const method = 'patch'
90110
return {
91-
url: createSignedUrlForRoute(identifier, params, options, 'PATCH'),
92-
method: 'patch',
111+
url,
112+
method,
93113
toString() {
94-
return this.url
114+
return url
115+
},
116+
form: {
117+
action: url,
118+
method,
95119
},
96120
}
97121
}
98122

99123
signedRoute.delete = function routeDelete(...[identifier, params, options]) {
124+
const url = createSignedUrlForRoute(identifier, params, options, 'DELETE')
125+
const method = 'delete'
100126
return {
101-
url: createSignedUrlForRoute(identifier, params, options, 'DELETE'),
102-
method: 'delete',
127+
url,
128+
method,
103129
toString() {
104-
return this.url
130+
return url
131+
},
132+
form: {
133+
action: url,
134+
method,
105135
},
106136
}
107137
}
108138

109139
signedRoute.method = function routeGet(method, ...[identifier, params, options]) {
140+
const url = createSignedUrlForRoute(identifier, params, options, method)
110141
return {
111-
url: createSignedUrlForRoute(identifier, params, options, method),
142+
url,
112143
method,
113144
toString() {
114-
return this.url
145+
return url
146+
},
147+
form: {
148+
action: url,
149+
method,
115150
},
116151
}
117152
}

src/router/url_builder.ts

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,61 +54,96 @@ export function createUrlBuilder<Routes extends LookupList>(
5454
}
5555

5656
urlFor.get = function urlForMethodGet(...[identifier, params, options]) {
57+
const url = createUrlForRoute(identifier, params, options, 'GET')
58+
const method = 'get'
5759
return {
58-
url: createUrlForRoute(identifier, params, options, 'GET'),
59-
method: 'get',
60+
url,
61+
method,
6062
toString() {
61-
return this.url
63+
return url
64+
},
65+
form: {
66+
action: url,
67+
method,
6268
},
6369
}
6470
}
6571

6672
urlFor.post = function urlForMethodPost(...[identifier, params, options]) {
73+
const url = createUrlForRoute(identifier, params, options, 'POST')
74+
const method = 'post'
6775
return {
68-
url: createUrlForRoute(identifier, params, options, 'POST'),
69-
method: 'post',
76+
url,
77+
method,
7078
toString() {
71-
return this.url
79+
return url
80+
},
81+
form: {
82+
action: url,
83+
method,
7284
},
7385
}
7486
}
7587

7688
urlFor.put = function urlForMethodPut(...[identifier, params, options]) {
89+
const url = createUrlForRoute(identifier, params, options, 'PUT')
90+
const method = 'put'
7791
return {
78-
url: createUrlForRoute(identifier, params, options, 'PUT'),
79-
method: 'put',
92+
url,
93+
method,
8094
toString() {
81-
return this.url
95+
return url
96+
},
97+
form: {
98+
action: url,
99+
method,
82100
},
83101
}
84102
}
85103

86104
urlFor.patch = function urlForMethodPatch(...[identifier, params, options]) {
105+
const url = createUrlForRoute(identifier, params, options, 'PATCH')
106+
const method = 'patch'
87107
return {
88-
url: createUrlForRoute(identifier, params, options, 'PATCH'),
89-
method: 'patch',
108+
url,
109+
method,
90110
toString() {
91-
return this.url
111+
return url
112+
},
113+
form: {
114+
action: url,
115+
method,
92116
},
93117
}
94118
}
95119

96120
urlFor.delete = function urlForMethodDelete(...[identifier, params, options]) {
121+
const url = createUrlForRoute(identifier, params, options, 'DELETE')
122+
const method = 'delete'
97123
return {
98-
url: createUrlForRoute(identifier, params, options, 'DELETE'),
99-
method: 'delete',
124+
url,
125+
method,
100126
toString() {
101-
return this.url
127+
return url
128+
},
129+
form: {
130+
action: url,
131+
method,
102132
},
103133
}
104134
}
105135

106136
urlFor.method = function urlForCustomMethod(method, ...[identifier, params, options]) {
137+
const url = createUrlForRoute(identifier, params, options, method)
107138
return {
108-
url: createUrlForRoute(identifier, params, options, method),
139+
url,
109140
method,
110141
toString() {
111-
return this.url
142+
return url
143+
},
144+
form: {
145+
action: url,
146+
method,
112147
},
113148
}
114149
}

src/types/url_builder.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export type UrlFor<Routes extends LookupList, Options extends any = URLOptions>
124124
Routes['GET'][RouteIdentifier],
125125
Options
126126
>
127-
): { method: 'get'; url: string }
127+
): { method: 'get'; url: string; form: { action: string; method: 'get' } }
128128

129129
/**
130130
* Make URL for a POST route. An error will be raised if the route doesn't
@@ -141,7 +141,7 @@ export type UrlFor<Routes extends LookupList, Options extends any = URLOptions>
141141
Routes['POST'][RouteIdentifier],
142142
Options
143143
>
144-
): { method: 'post'; url: string }
144+
): { method: 'post'; url: string; form: { action: string; method: 'post' } }
145145

146146
/**
147147
* Make URL for a PUT route. An error will be raised if the route doesn't
@@ -158,7 +158,7 @@ export type UrlFor<Routes extends LookupList, Options extends any = URLOptions>
158158
Routes['PUT'][RouteIdentifier],
159159
Options
160160
>
161-
): { method: 'put'; url: string }
161+
): { method: 'put'; url: string; form: { action: string; method: 'put' } }
162162

163163
/**
164164
* Make URL for a PATCH route. An error will be raised if the route doesn't
@@ -175,7 +175,7 @@ export type UrlFor<Routes extends LookupList, Options extends any = URLOptions>
175175
Routes['PATCH'][RouteIdentifier],
176176
Options
177177
>
178-
): { method: 'patch'; url: string }
178+
): { method: 'patch'; url: string; form: { action: string; method: 'patch' } }
179179

180180
/**
181181
* Make URL for a DELETE route. An error will be raised if the route doesn't
@@ -192,7 +192,7 @@ export type UrlFor<Routes extends LookupList, Options extends any = URLOptions>
192192
Routes['DELETE'][RouteIdentifier],
193193
Options
194194
>
195-
): { method: 'delete'; url: string }
195+
): { method: 'delete'; url: string; form: { action: string; method: 'delete' } }
196196

197197
/**
198198
* Make URL for a custom route method. An error will be raised if the route doesn't
@@ -208,7 +208,7 @@ export type UrlFor<Routes extends LookupList, Options extends any = URLOptions>
208208
Routes[Method][RouteIdentifier],
209209
Options
210210
>
211-
): { method: Method; url: string }
211+
): { method: Method; url: string; form: { action: string; method: Method } }
212212
}
213213

214214
/**

tests/router/url_builder.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,36 +88,60 @@ test.group('URLBuilder', () => {
8888
router.commit()
8989

9090
assert.containSubset(urlFor.get('users.show', { id: '1' }), { method: 'get', url: '/users/1' })
91+
assert.deepEqual(urlFor.get('users.show', { id: '1' }).form, {
92+
method: 'get',
93+
action: '/users/1',
94+
})
9195
assert.equal(`${urlFor.get('users.show', { id: '1' })}`, '/users/1')
9296

9397
assert.containSubset(urlFor.post('users.index'), {
9498
method: 'post',
9599
url: '/users',
96100
})
101+
assert.deepEqual(urlFor.post('users.index').form, {
102+
method: 'post',
103+
action: '/users',
104+
})
97105
assert.equal(`${urlFor.post('users.index')}`, '/users')
98106

99107
assert.containSubset(urlFor.put('users.show', { id: '1' }), {
100108
method: 'put',
101109
url: '/users/1',
102110
})
111+
assert.deepEqual(urlFor.put('users.show', { id: '1' }).form, {
112+
method: 'put',
113+
action: '/users/1',
114+
})
103115
assert.equal(`${urlFor.put('users.show', { id: '1' })}`, '/users/1')
104116

105117
assert.containSubset(urlFor.patch('users.show', { id: '1' }), {
106118
method: 'patch',
107119
url: '/users/1',
108120
})
121+
assert.deepEqual(urlFor.patch('users.show', { id: '1' }).form, {
122+
method: 'patch',
123+
action: '/users/1',
124+
})
109125
assert.equal(`${urlFor.patch('users.show', { id: '1' })}`, '/users/1')
110126

111127
assert.containSubset(urlFor.delete('users.show', { id: '1' }), {
112128
method: 'delete',
113129
url: '/users/1',
114130
})
131+
assert.deepEqual(urlFor.delete('users.show', { id: '1' }).form, {
132+
method: 'delete',
133+
action: '/users/1',
134+
})
115135
assert.equal(`${urlFor.delete('users.show', { id: '1' })}`, '/users/1')
116136

117137
assert.containSubset(urlFor.method('GET', 'users.show', { id: '1' }), {
118138
method: 'GET',
119139
url: '/users/1',
120140
})
141+
assert.containSubset(urlFor.method('GET', 'users.show', { id: '1' }).form, {
142+
method: 'GET',
143+
action: '/users/1',
144+
})
121145
assert.equal(`${urlFor.method('GET', 'users.show', { id: '1' })}`, '/users/1')
122146
})
123147

0 commit comments

Comments
 (0)