rbs_ts_generator 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +10 -47
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e0bdcb75fbd43e55803cc7c21203215a2cba5a42d840dc925b595767a947138
4
- data.tar.gz: 9da00247066e1b51919e4619b3fdc290a5f25a8772ff1c358836a3f0fa71c445
3
+ metadata.gz: 80e481b7939151a064e8dd75a83ffa7f2bb886259f783d19143d92fab10abd98
4
+ data.tar.gz: 1718516288d3fe9201da07b2883dd83a957de2db6b494401615365aa2c7c78df
5
5
  SHA512:
6
- metadata.gz: 0bd4417dadbb8f9b18c6f3dc4d3e85f80cd0f7dad4954db298f49c58048ddd7d5341e37361aead84038132b9068127e74a5241e8dfea9515d1508b0188db00e2
7
- data.tar.gz: d6f7285ff572d6ce0e1854a34c01ac62dd9ec763fdc13b7f06579efd050cff0c1a788bda584682910697a146f79249bfd241779d963e336f5fb3449e64b1ef7a
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) -> ({ url: String, message: String } | Array[String])
19
- def update: (Integer id, String title) -> ({ url: String, message: String } | Array[String])
20
- def destroy: (Integer id) -> { url: String, message: String }
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) -> ({ url: String, message: String } | Array[String] | void)
35
- def update: (Integer id, String title) -> ({ url: String, message: String } | Array[String] | void)
36
- def destroy: (Integer id) -> ({ url: String, message: String } | void)
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.17.1] [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/mailers/application_mailer.rb, app/models/application_record.rb, app/models/board.rb, app/mailboxes/application_mailbox.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.
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' as const, boards, params).then(({ json }) => {
122
+ railsApi('POST', boards, params).then(({ json }) => {
160
123
  if (json instanceof Array) {
161
124
  return Promise.reject(json)
162
125
  } else {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbs_ts_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seiei Miyagi