-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
95 lines (84 loc) · 2.57 KB
/
Copy pathclient.ts
File metadata and controls
95 lines (84 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Builds the template context for client files
// (lib/seam/routes/{snake_name}.rb).
// Mirrors the output of the nextlove RubyClient#serialize.
import {
type ClientMethod,
type ClientModel,
sortClientMethodParameters,
} from '../ruby-client.js'
export interface ClientMethodLayoutContext {
description: string
isDeprecated: boolean
deprecationMessage: string
responseDescription: string
parameters: ClientMethod['parameters']
name: string
hasSignature: boolean
signatureParams: string
hasParams: boolean
bodyParams: string
path: string
usesRes: boolean
isResource: boolean
isPoll: boolean
isNil: boolean
returnResource: string
returnPath: string
}
export interface ClientLayoutContext {
className: string
importActionAttempt: boolean
childClients: Array<{ namespace: string; clientName: string }>
methods: ClientMethodLayoutContext[]
}
const getMethodLayoutContext = (
method: ClientMethod,
): ClientMethodLayoutContext => {
const { methodName, path, parameters, returnResource, returnPath } = method
const hasReturnValue = returnResource != null && returnPath !== ''
const canPollActionAttempt = returnPath === 'action_attempt'
const hasParams = parameters.length > 0
const sortedParameters = sortClientMethodParameters(parameters)
const signatureParams = sortedParameters
.map((p) => `${p.name}${p.required ?? false ? ':' : ': nil'}`)
.concat(canPollActionAttempt ? ['wait_for_action_attempt: nil'] : [])
.join(', ')
const bodyParams = `{${sortedParameters
.map((p) => `${p.name}: ${p.name}`)
.join(', ')}}`
const isResource = hasReturnValue && !canPollActionAttempt
const isPoll = canPollActionAttempt
const isNil = !hasReturnValue
return {
description: method.description,
isDeprecated: method.isDeprecated,
deprecationMessage: method.deprecationMessage,
responseDescription: method.responseDescription,
parameters: sortedParameters,
name: methodName,
hasSignature: signatureParams.length > 0,
signatureParams,
hasParams,
bodyParams,
path,
usesRes: isResource || isPoll,
isResource,
isPoll,
isNil,
returnResource: returnResource ?? '',
returnPath,
}
}
export const setClientLayoutContext = (
cls: ClientModel,
): ClientLayoutContext => ({
className: cls.name,
importActionAttempt: cls.methods.some(
({ returnResource }) => returnResource === 'ActionAttempt',
),
childClients: cls.childClientIdentifiers.map((i) => ({
namespace: i.namespace,
clientName: i.clientName,
})),
methods: cls.methods.map(getMethodLayoutContext),
})