foobara-typescript-remote-command-generator 0.0.13 → 0.0.15

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5045b4f54238822deb4764d5ea2f259b482c3350c20e348472b20d30fd1feb83
4
- data.tar.gz: 24187f3f79dc8fcd275ff4a8b98010dbac41128b4a6dd92faaf073544746eb7c
3
+ metadata.gz: 30a3b38f66be2984ac2e6db57d53fe9abf21def7569fc0472938a0166f83934c
4
+ data.tar.gz: afb81264f3fb218f0e596ad946c77ea509c8fa1e4c99816c2c77e64740892b22
5
5
  SHA512:
6
- metadata.gz: ae7f0d944f64a64faf54c8cc78f9d0afc53f9c4e857c75efff8993c3132ecefcaa96aec799909444f80dd38b3023db947ab3381a116f298e365b2fb0455432e3
7
- data.tar.gz: 8b8b124b10f82ce701b48216f338561b7104d44b45f0793148df2d0545d41947cde9d5b12dd2dea4796c9a5c79005d4f9489ebc6f52cbf68d39cf1698b863b26
6
+ metadata.gz: 469376f5e5e6f22706a954172b8e60610d1c390b74eb8a3f038c56229459cb292893b8ee6e29caa45b398d96f6fcc81e5165517a88c1f82df415fb94e63b1aaa
7
+ data.tar.gz: 0dd2303b15e531e72deed4a4d876d1e5e1dbe5d5cdb85f4577ebeb1fcaea0bc0c33be0fede4f5aededbd1c4c965cc07b565a789bad7fd56852ee675994972bac
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## [0.0.15] - 2025-03-30
2
+
3
+ - Better error handling in RemoteCommand
4
+
5
+ ## [0.0.14] - 2025-03-30
6
+
7
+ - Implement Auth domain support in RemoteCommand
8
+
1
9
  ## [0.0.13] - 2025-03-17
2
10
 
3
11
  - Fix bug incorrectly generating model typescript for detached entities
@@ -1,13 +1,16 @@
1
1
  import { type Outcome, SuccessfulOutcome, ErrorOutcome } from './Outcome'
2
2
  import { type FoobaraError } from './Error'
3
3
 
4
- export type commandState = 'initialized' | 'executing' | 'succeeded' | 'errored' | 'failed'
4
+ export type commandState = 'initialized' | 'executing' | 'refreshing_auth' | 'succeeded' | 'errored' | 'failed'
5
+
6
+ const accessTokens: Record<string, string> = {}
5
7
 
6
8
  export default abstract class RemoteCommand<Inputs, Result, CommandError extends FoobaraError> {
7
9
  static _urlBase: string | undefined
8
10
  static commandName: string
9
11
  static organizationName: string
10
12
  static domainName: string
13
+ static authRequired: boolean = true // TODO: set this from generator
11
14
 
12
15
  // TODO: make use of domain's config instead of process.env directly.
13
16
  static get urlBase (): string {
@@ -73,27 +76,70 @@ export default abstract class RemoteCommand<Inputs, Result, CommandError extends
73
76
  return (this.constructor as typeof RemoteCommand<Inputs, Result, CommandError>).fullCommandName
74
77
  }
75
78
 
79
+ get commandPath (): string {
80
+ return this.fullCommandName.replaceAll('::', '/')
81
+ }
82
+
76
83
  async run (): Promise<Outcome<Result, CommandError>> {
77
- const url = `${this.urlBase}/run/${this.fullCommandName}`
84
+ const url = `${this.urlBase}/run/${this.commandPath}`
78
85
 
79
- this.commandState = 'executing'
80
- const response = await fetch(url, {
86
+ const bearerToken = accessTokens[this.urlBase]
87
+
88
+ const requestParams: RequestInit = {
81
89
  method: 'POST',
82
- headers: { 'Content-Type': 'application/json' },
83
- body: JSON.stringify(this.inputs)
84
- })
90
+ body: JSON.stringify(this.inputs),
91
+ credentials: 'include'
92
+ }
93
+
94
+ requestParams.headers = { 'Content-Type': 'application/json' }
85
95
 
86
- const body = await response.json()
96
+ if (bearerToken != null) {
97
+ requestParams.headers.Authorization = `Bearer ${bearerToken}`
98
+ }
99
+
100
+ this.commandState = 'executing'
101
+ let response = await fetch(url, requestParams)
102
+
103
+ if ((this.constructor as typeof RemoteCommand<Inputs, Result, CommandError>).authRequired &&
104
+ response.status === 401) {
105
+ this.commandState = 'refreshing_auth'
106
+
107
+ // TODO: in generator make this conditional
108
+ const { RefreshLogin } = await import('../Foobara/Auth')
109
+ // See if we can authenticate using the refresh token
110
+ const refreshCommand = new RefreshLogin({})
111
+ const outcome = await refreshCommand.run()
112
+
113
+ if (outcome.isSuccess()) {
114
+ const bearerToken = accessTokens[this.urlBase]
115
+
116
+ if (bearerToken != null) {
117
+ requestParams.headers.Authorization = `Bearer ${bearerToken}`
118
+ }
119
+
120
+ this.commandState = 'executing'
121
+ response = await fetch(url, requestParams)
122
+ }
123
+ }
124
+
125
+ const text = await response.text()
126
+ const body = JSON.parse(text)
87
127
 
88
128
  if (response.ok) {
129
+ const accessToken: string | null = response.headers.get('X-Access-Token')
130
+
131
+ if (accessToken != null) {
132
+ accessTokens[this.urlBase] = accessToken
133
+ }
134
+
89
135
  this.commandState = 'succeeded'
90
136
  this.outcome = new SuccessfulOutcome<Result, CommandError>(body)
91
- } else if (response.status === 422) {
137
+ } else if (response.status === 422 || response.status === 401 || response.status === 403) {
92
138
  this.commandState = 'errored'
93
139
  this.outcome = new ErrorOutcome<Result, CommandError>(body)
94
140
  } else {
95
141
  this.commandState = 'failed'
96
- throw new Error(`not sure how to handle ${await response.text()}`)
142
+ throw new Error(`not sure how to handle ${text}`)
97
143
  }
98
144
 
99
145
  return this.outcome
metadata CHANGED
@@ -1,42 +1,42 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foobara-typescript-remote-command-generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Georgi
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-03-17 00:00:00.000000000 Z
10
+ date: 2025-03-31 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: foobara
14
14
  requirement: !ruby/object:Gem::Requirement
15
15
  requirements:
16
- - - ">="
16
+ - - "~>"
17
17
  - !ruby/object:Gem::Version
18
- version: '0'
18
+ version: 0.0.88
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
- - - ">="
23
+ - - "~>"
24
24
  - !ruby/object:Gem::Version
25
- version: '0'
25
+ version: 0.0.88
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: foobara-files-generator
28
28
  requirement: !ruby/object:Gem::Requirement
29
29
  requirements:
30
- - - ">="
30
+ - - "~>"
31
31
  - !ruby/object:Gem::Version
32
- version: '0'
32
+ version: 0.0.1
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - ">="
37
+ - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '0'
39
+ version: 0.0.1
40
40
  email:
41
41
  - azimux@gmail.com
42
42
  executables: []
@@ -134,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
134
  - !ruby/object:Gem::Version
135
135
  version: '0'
136
136
  requirements: []
137
- rubygems_version: 3.6.5
137
+ rubygems_version: 3.6.6
138
138
  specification_version: 4
139
139
  summary: Generates remote commands for Typescript from a foobara manifest
140
140
  test_files: []