File tree Expand file tree Collapse file tree 4 files changed +52
-2
lines changed
packages/kubernetes-client/src/fetch Expand file tree Collapse file tree 4 files changed +52
-2
lines changed Original file line number Diff line number Diff line change @@ -3,3 +3,4 @@ export * from './builder'
3
3
export * from './client'
4
4
export * from './urlgenerator'
5
5
export * from './authorizer'
6
+ export * from './pluralizer'
Original file line number Diff line number Diff line change
1
+ import { describe , expect , it } from 'vitest'
2
+ import { DefaultPluralizer } from './pluralizer'
3
+
4
+ describe ( 'Pluralizer' , ( ) => {
5
+ const tests : {
6
+ name : string
7
+ input : string
8
+ expected : string
9
+ } [ ] = [
10
+ {
11
+ name : 'should append s to kind without y suffix' ,
12
+ input : 'namespace' ,
13
+ expected : 'namespaces' ,
14
+ } ,
15
+ {
16
+ name : 'should replace last occurrence of y with ie' ,
17
+ input : 'happyentity' ,
18
+ expected : 'happyentities' ,
19
+ } ,
20
+ ]
21
+
22
+ tests . forEach ( ( { name, input, expected } ) => {
23
+ it ( name , ( ) => {
24
+ const p = new DefaultPluralizer ( )
25
+ const result = p . pluralize ( input )
26
+ expect ( result ) . toEqual ( expected )
27
+ } )
28
+ } )
29
+ } )
Original file line number Diff line number Diff line change
1
+ export interface Pluralizer {
2
+ /**
3
+ * Returns the plural form of the given kind.
4
+ * Required for building an URL endpoint.
5
+ * @param kind API kind as given in `kind` field of a resource, but in lower-case.
6
+ * @returns kind in lower-case in plural form.
7
+ */
8
+ pluralize ( kind : string ) : string
9
+ }
10
+
11
+ /**
12
+ * Appends an "s" to the `kind`.
13
+ * If `kind` ends with "y", it is replaced with "ie" before appending an "s".
14
+ */
15
+ export class DefaultPluralizer implements Pluralizer {
16
+ pluralize ( kind : string ) : string {
17
+ return kind . replace ( / y $ / , 'ie' ) . concat ( 's' )
18
+ }
19
+ }
Original file line number Diff line number Diff line change 1
1
import { OptionValue } from '../api'
2
+ import { DefaultPluralizer , Pluralizer } from './pluralizer'
2
3
3
4
export declare type HttpMethods = 'DELETE' | 'GET' | 'POST' | 'PUT' | 'PATCH'
4
5
@@ -21,7 +22,7 @@ export interface UrlGenerator {
21
22
* Default {@link UrlGenerator} that generates API endpoint paths based on resource metadata.
22
23
*/
23
24
export class KubernetesUrlGenerator implements UrlGenerator {
24
- constructor ( private apiBase = '' ) { }
25
+ constructor ( private apiBase = '' , private pluralizer : Pluralizer = new DefaultPluralizer ( ) ) { }
25
26
26
27
buildEndpoint (
27
28
method : HttpMethods ,
@@ -43,7 +44,7 @@ export class KubernetesUrlGenerator implements UrlGenerator {
43
44
endpoint . push ( 'namespaces' )
44
45
endpoint . push ( inNamespace )
45
46
}
46
- endpoint . push ( kind . toLowerCase ( ) . concat ( 's' ) )
47
+ endpoint . push ( this . pluralizer . pluralize ( kind . toLowerCase ( ) ) )
47
48
if ( name && name !== '' && method !== 'POST' ) {
48
49
endpoint . push ( name )
49
50
}
You can’t perform that action at this time.
0 commit comments