@kubb/plugin-msw 5.0.0-beta.10 → 5.0.0-beta.101

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,28 +0,0 @@
1
- import { camelCase } from '@internals/utils'
2
- import { defineResolver } from '@kubb/core'
3
- import type { PluginMsw } from '../types.ts'
4
-
5
- /**
6
- * Naming convention resolver for MSW plugin.
7
- *
8
- * Provides default naming helpers using camelCase with a `handler` suffix.
9
- */
10
- export const resolverMsw = defineResolver<PluginMsw>(() => ({
11
- name: 'default',
12
- pluginName: 'plugin-msw',
13
- default(name, type) {
14
- return camelCase(name, { isFile: type === 'file' })
15
- },
16
- resolveName(name) {
17
- return camelCase(name, { suffix: 'handler' })
18
- },
19
- resolvePathName(name, type) {
20
- return this.default(name, type)
21
- },
22
- resolveHandlerName(node) {
23
- return this.resolveName(node.operationId)
24
- },
25
- resolveHandlersName() {
26
- return 'handlers'
27
- },
28
- }))
package/src/types.ts DELETED
@@ -1,93 +0,0 @@
1
- import type { ast, Exclude, Generator, Group, Include, Output, Override, PluginFactoryOptions, Resolver } from '@kubb/core'
2
-
3
- /**
4
- * Resolver for MSW that provides naming methods for handler functions.
5
- */
6
- export type ResolverMsw = Resolver & {
7
- /**
8
- * Resolves the base handler function name for an operation.
9
- */
10
- resolveName(this: ResolverMsw, name: string): string
11
- /**
12
- * Resolves the output file name for an MSW handler module.
13
- */
14
- resolvePathName(this: ResolverMsw, name: string, type?: 'file' | 'function' | 'type' | 'const'): string
15
- /**
16
- * Resolves the handler function name for an operation.
17
- */
18
- resolveHandlerName(this: ResolverMsw, node: ast.OperationNode): string
19
- /**
20
- * Resolves the exported handlers collection name.
21
- */
22
- resolveHandlersName(this: ResolverMsw): string
23
- }
24
-
25
- export type Options = {
26
- /**
27
- * Specify the export location for the files and define the behavior of the output
28
- * @default { path: 'handlers', barrelType: 'named' }
29
- */
30
- output?: Output
31
- baseURL?: string
32
- /**
33
- * Group the MSW mocks based on the provided name.
34
- */
35
- group?: Group
36
- /**
37
- * Tags, operations, or paths to exclude from generation.
38
- */
39
- exclude?: Array<Exclude>
40
- /**
41
- * Tags, operations, or paths to include in generation.
42
- */
43
- include?: Array<Include>
44
- /**
45
- * Override options for specific tags, operations, or paths.
46
- */
47
- override?: Array<Override<ResolvedOptions>>
48
- /**
49
- * Override naming conventions for function names and types.
50
- */
51
- resolver?: Partial<ResolverMsw> & ThisType<ResolverMsw>
52
- /**
53
- * AST visitor to transform generated nodes.
54
- */
55
- transformer?: ast.Visitor
56
- /**
57
- * Create `handlers.ts` file with all handlers grouped by methods.
58
- * @default false
59
- */
60
- handlers?: boolean
61
- /**
62
- * Which parser to use for generating response data.
63
- *
64
- * @default 'data'
65
- */
66
- parser?: 'data' | 'faker'
67
- /**
68
- * Additional generators alongside the default generators.
69
- */
70
- generators?: Array<Generator<PluginMsw>>
71
- }
72
-
73
- type ResolvedOptions = {
74
- output: Output
75
- group: Group | undefined
76
- exclude: NonNullable<Options['exclude']>
77
- include: Options['include']
78
- override: NonNullable<Options['override']>
79
- parser: NonNullable<Options['parser']>
80
- baseURL: Options['baseURL'] | undefined
81
- handlers: boolean
82
- resolver: ResolverMsw
83
- }
84
-
85
- export type PluginMsw = PluginFactoryOptions<'plugin-msw', Options, ResolvedOptions, ResolverMsw>
86
-
87
- declare global {
88
- namespace Kubb {
89
- interface PluginRegistry {
90
- 'plugin-msw': PluginMsw
91
- }
92
- }
93
- }
package/src/utils.ts DELETED
@@ -1,58 +0,0 @@
1
- import type { ast } from '@kubb/core'
2
- import type { ResolverFaker } from '@kubb/plugin-faker'
3
- import type { PluginMsw } from './types.ts'
4
-
5
- /**
6
- * Gets the content type from a response, defaulting to 'application/json' if a schema exists.
7
- */
8
- export function getContentType(response: ast.ResponseNode | undefined): string | undefined {
9
- return getResponseContentType(response) ?? (hasResponseSchema(response) ? 'application/json' : undefined)
10
- }
11
-
12
- /**
13
- * Determines if a response has a schema that is not void or any.
14
- */
15
- export function hasResponseSchema(response: ast.ResponseNode | undefined): boolean {
16
- return !!getResponseContentType(response) || (!!response?.schema && response.schema.type !== 'void' && response.schema.type !== 'any')
17
- }
18
-
19
- function getResponseContentType(response: ast.ResponseNode | undefined): string | undefined {
20
- const contentType = response as unknown as { mediaType?: string | null; contentType?: string | null } | undefined
21
- const value = contentType?.mediaType ?? contentType?.contentType
22
- return typeof value === 'string' && value.length > 0 ? value : undefined
23
- }
24
-
25
- /**
26
- * Converts an HTTP method to its lowercase MSW equivalent (e.g., 'POST' → 'post').
27
- */
28
- export function getMswMethod(node: ast.OperationNode): string {
29
- return node.method.toLowerCase()
30
- }
31
-
32
- /**
33
- * Converts an OpenAPI-style path to an Express/MSW-style path by replacing `{param}` with `:param`.
34
- */
35
- export function getMswUrl(node: ast.OperationNode): string {
36
- return node.path.replaceAll('{', ':').replaceAll('}', '')
37
- }
38
-
39
- /**
40
- * Resolves faker metadata for an MSW operation, including response name and file path.
41
- */
42
- export function resolveFakerMeta(
43
- node: ast.OperationNode,
44
- options: {
45
- root: string
46
- fakerResolver: ResolverFaker
47
- fakerOutput: PluginMsw['resolvedOptions']['output']
48
- fakerGroup: PluginMsw['resolvedOptions']['group']
49
- },
50
- ): { name: string; file: { path: string } } {
51
- const { root, fakerResolver, fakerOutput, fakerGroup } = options
52
- const tag = node.tags[0] ?? 'default'
53
-
54
- return {
55
- name: fakerResolver.resolveResponseName(node),
56
- file: fakerResolver.resolveFile({ name: node.operationId, extname: '.ts', tag, path: node.path }, { root, output: fakerOutput, group: fakerGroup }),
57
- }
58
- }