rbs_ts_generator 0.2.0 → 0.2.1
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.
- checksums.yaml +4 -4
- data/README.md +10 -47
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 80e481b7939151a064e8dd75a83ffa7f2bb886259f783d19143d92fab10abd98
|
4
|
+
data.tar.gz: 1718516288d3fe9201da07b2883dd83a957de2db6b494401615365aa2c7c78df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54c0d8b7c1a489c78a498a4e4350c0b58b11fc47733f5455894ef96846eb4491e270b487c940139f8efb75b8d1667030f1f92299d55fb987920d5fa5079160bd
|
7
|
+
data.tar.gz: 7bed37d5dfe2bfae016bd92fc9747bcee462f93b93ad2629b1bf463be9bb2f2af53398850045165c7fd88c8ab224ae7c540c71a3b7ea007215fc057c0d861f14
|
data/README.md
CHANGED
@@ -10,14 +10,14 @@ Write type signature of your controller actions in [ruby/rbs](https://github.com
|
|
10
10
|
|
11
11
|
```rbs
|
12
12
|
# sig/app/controllers/boards_controller.rbs
|
13
|
-
class BoardsController < ApplicationController
|
13
|
+
class BoardsController < ::ApplicationController
|
14
14
|
@board: Board
|
15
15
|
@boards: Board::ActiveRecord_Relation
|
16
16
|
|
17
17
|
def index: () -> Array[{ id: Integer, title: String }]
|
18
|
-
def create: (String title) ->
|
19
|
-
def update: (Integer id, String title) ->
|
20
|
-
def destroy: (Integer id) -> {
|
18
|
+
def create: (String title) -> { id: Integer, message: String } | Array[String]
|
19
|
+
def update: (Integer id, String title) -> { id: Integer, message: String } | Array[String]
|
20
|
+
def destroy: (Integer id) -> { message: String }
|
21
21
|
end
|
22
22
|
```
|
23
23
|
|
@@ -26,14 +26,14 @@ But action does not explicitly return json record.
|
|
26
26
|
To pass the ruby type checking, add `| void` to each signatures.
|
27
27
|
|
28
28
|
```rbs
|
29
|
-
class BoardsController < ApplicationController
|
29
|
+
class BoardsController < ::ApplicationController
|
30
30
|
@board: Board
|
31
31
|
@boards: Board::ActiveRecord_Relation
|
32
32
|
|
33
33
|
def index: () -> (Array[{ id: Integer, title: String }] | void)
|
34
|
-
def create: (String title) -> ({
|
35
|
-
def update: (Integer id, String title) -> ({
|
36
|
-
def destroy: (Integer id) -> ({
|
34
|
+
def create: (String title) -> ({ id: Integer, message: String } | Array[String] | void)
|
35
|
+
def update: (Integer id, String title) -> ({ id: Integer, message: String } | Array[String] | void)
|
36
|
+
def destroy: (Integer id) -> ({ message: String } | void)
|
37
37
|
end
|
38
38
|
```
|
39
39
|
|
@@ -52,7 +52,7 @@ end
|
|
52
52
|
|
53
53
|
```console
|
54
54
|
$ bundle exec steep check
|
55
|
-
[Steep 0.
|
55
|
+
[Steep 0.20.0] [target=app] [target#type_check(target_sources: [app/channels/application_cable/channel.rb, app/channels/application_cable/connection.rb, app/controllers/application_controller.rb, app/controllers/boards_controller.rb, app/helpers/application_helper.rb, app/helpers/boards_helper.rb, app/jobs/application_job.rb, app/mailboxes/application_mailbox.rb, app/mailers/application_mailer.rb, app/models/application_record.rb, app/models/board.rb], validate_signatures: true)] [synthesize:(1:1)] [synthesize:(2:3)] [synthesize:(2:3)] [(*::Symbol, ?model_name: ::string, **untyped) -> void] Method call with rest keywords type is detected. Rough approximation to be improved.
|
56
56
|
```
|
57
57
|
|
58
58
|
When you passed the ruby type check, next generate TypeScript from those signatures.
|
@@ -112,43 +112,6 @@ export const board = {
|
|
112
112
|
|
113
113
|
And generate default runtime in `app/javascript/packs/rbs_ts_runtime.ts`
|
114
114
|
|
115
|
-
```typescript
|
116
|
-
type HttpMethods = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE'
|
117
|
-
type BaseResource = {
|
118
|
-
path: (args: any) => string
|
119
|
-
names: string[]
|
120
|
-
Methods?: any
|
121
|
-
Params?: { [method in HttpMethods]?: any }
|
122
|
-
Return?: { [method in HttpMethods]?: any }
|
123
|
-
}
|
124
|
-
export async function railsApi<
|
125
|
-
Method extends Exclude<Resource['Methods'], undefined>,
|
126
|
-
Resource extends BaseResource,
|
127
|
-
Params extends Exclude<Resource['Params'], undefined>[Method],
|
128
|
-
Return extends Exclude<Resource['Return'], undefined>[Method]
|
129
|
-
>(method: Method, { path, names }: Resource, params: Params): Promise<{ status: number, json: Return }> {
|
130
|
-
const tag = document.querySelector<HTMLMetaElement>('meta[name=csrf-token]')
|
131
|
-
const paramsNotInNames = Object.keys(params).reduce<object>((ps, key) => names.indexOf(key) === - 1 ? { ...ps, [key]: params[key] } : ps, {})
|
132
|
-
const searchParams = new URLSearchParams()
|
133
|
-
for (const name of Object.keys(paramsNotInNames)) {
|
134
|
-
searchParams.append(name, paramsNotInNames[name])
|
135
|
-
}
|
136
|
-
const query = method === 'GET' && Object.keys(paramsNotInNames).length ? `?${searchParams.toString()}` : ''
|
137
|
-
const body = method === 'GET' ? undefined : JSON.stringify(paramsNotInNames)
|
138
|
-
const response = await fetch(path(params) + query, {
|
139
|
-
method,
|
140
|
-
body,
|
141
|
-
headers: {
|
142
|
-
'Accept': 'application/json',
|
143
|
-
'Content-Type': 'application/json',
|
144
|
-
'X-CSRF-Token': tag.content
|
145
|
-
}
|
146
|
-
})
|
147
|
-
const json = await response.json() as Return
|
148
|
-
return new Promise((resolve) => resolve({ status: response.status, json: json }))
|
149
|
-
}
|
150
|
-
```
|
151
|
-
|
152
115
|
In your TypeScript code, you can use those routes definition and the default runtime like following
|
153
116
|
|
154
117
|
```typescript
|
@@ -156,7 +119,7 @@ import { boards } from './rbs_ts_routes'
|
|
156
119
|
import { railsApi } from './rbs_ts_runtime'
|
157
120
|
|
158
121
|
const params = { title: 'test' }
|
159
|
-
railsApi('POST'
|
122
|
+
railsApi('POST', boards, params).then(({ json }) => {
|
160
123
|
if (json instanceof Array) {
|
161
124
|
return Promise.reject(json)
|
162
125
|
} else {
|