Skip to content

Commit 6b5cec5

Browse files
authored
Merge pull request #45 from ccremer/plurals
Add pluralizer to fetch implementation
2 parents 6a142b0 + 4cec5a3 commit 6b5cec5

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

packages/kubernetes-client/src/fetch/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './builder'
33
export * from './client'
44
export * from './urlgenerator'
55
export * from './authorizer'
6+
export * from './pluralizer'
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
})
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}

packages/kubernetes-client/src/fetch/urlgenerator.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { OptionValue } from '../api'
2+
import { DefaultPluralizer, Pluralizer } from './pluralizer'
23

34
export declare type HttpMethods = 'DELETE' | 'GET' | 'POST' | 'PUT' | 'PATCH'
45

@@ -21,7 +22,7 @@ export interface UrlGenerator {
2122
* Default {@link UrlGenerator} that generates API endpoint paths based on resource metadata.
2223
*/
2324
export class KubernetesUrlGenerator implements UrlGenerator {
24-
constructor(private apiBase = '') {}
25+
constructor(private apiBase = '', private pluralizer: Pluralizer = new DefaultPluralizer()) {}
2526

2627
buildEndpoint(
2728
method: HttpMethods,
@@ -43,7 +44,7 @@ export class KubernetesUrlGenerator implements UrlGenerator {
4344
endpoint.push('namespaces')
4445
endpoint.push(inNamespace)
4546
}
46-
endpoint.push(kind.toLowerCase().concat('s'))
47+
endpoint.push(this.pluralizer.pluralize(kind.toLowerCase()))
4748
if (name && name !== '' && method !== 'POST') {
4849
endpoint.push(name)
4950
}

0 commit comments

Comments
 (0)