@nage-api/compat 1.0.0-beta.2
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.
- package/LICENSE +202 -0
- package/README.md +215 -0
- package/dist/cli.d.ts +9 -0
- package/dist/cli.js +29 -0
- package/dist/codemods/cli-main.d.ts +32 -0
- package/dist/codemods/cli-main.js +128 -0
- package/dist/codemods/config-inventory.d.ts +20 -0
- package/dist/codemods/config-inventory.js +118 -0
- package/dist/codemods/env-rename.d.ts +36 -0
- package/dist/codemods/env-rename.js +104 -0
- package/dist/codemods/import-paths.d.ts +45 -0
- package/dist/codemods/import-paths.js +247 -0
- package/dist/codemods/job-generics.d.ts +17 -0
- package/dist/codemods/job-generics.js +74 -0
- package/dist/codemods/node-fs.d.ts +11 -0
- package/dist/codemods/node-fs.js +35 -0
- package/dist/codemods/registry.d.ts +26 -0
- package/dist/codemods/registry.js +42 -0
- package/dist/codemods/res-envelope.d.ts +29 -0
- package/dist/codemods/res-envelope.js +111 -0
- package/dist/codemods/runner.d.ts +36 -0
- package/dist/codemods/runner.js +44 -0
- package/dist/codemods/types.d.ts +69 -0
- package/dist/codemods/types.js +40 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +63 -0
- package/dist/legacy/compat.module.d.ts +14 -0
- package/dist/legacy/compat.module.js +39 -0
- package/dist/legacy/envelope.d.ts +62 -0
- package/dist/legacy/envelope.js +86 -0
- package/dist/legacy/legacy-envelope.decorator.d.ts +17 -0
- package/dist/legacy/legacy-envelope.decorator.js +23 -0
- package/dist/legacy/legacy-envelope.interceptor.d.ts +25 -0
- package/dist/legacy/legacy-envelope.interceptor.js +67 -0
- package/dist/legacy/options.d.ts +14 -0
- package/dist/legacy/options.js +7 -0
- package/dist/legacy/responses.d.ts +48 -0
- package/dist/legacy/responses.js +52 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
# @nage-api/compat
|
|
2
|
+
|
|
3
|
+
Migration tooling for an application moving off `nest-core-v2` (PLAN.md §23, §25 P1).
|
|
4
|
+
|
|
5
|
+
Two halves that share nothing but a release:
|
|
6
|
+
|
|
7
|
+
- a **runtime shim** that lets a ported controller keep answering old clients in
|
|
8
|
+
the old shape, so the controller and its callers can move weeks apart;
|
|
9
|
+
- **codemods**, a build-time tool that an application never loads.
|
|
10
|
+
|
|
11
|
+
The package is temporary by design. When no route carries `@LegacyEnvelope()`
|
|
12
|
+
any more, delete the import and the dependency; nothing else holds it in place.
|
|
13
|
+
|
|
14
|
+
## Which output to trust
|
|
15
|
+
|
|
16
|
+
Every codemod declares a `kind`, and the distinction is the whole point of this
|
|
17
|
+
surface. A codemod that is right most of the time is worse than no codemod: it
|
|
18
|
+
produces a large, plausible diff that gets approved by scrolling.
|
|
19
|
+
|
|
20
|
+
| Codemod | Kind | What it does |
|
|
21
|
+
| ------------------ | ----------- | --------------------------------------------------------------------------------------------------------------------------------------- |
|
|
22
|
+
| `import-paths` | **rewrite** | Repoints `…/core.responses` at `@nage-api/compat` and `…/core.decorators` at `@nage-api/core`. Reports the other eleven legacy modules. |
|
|
23
|
+
| `env-rename` | **rewrite** | Fixes the `RECAPTCHA_SECRET_KET` typo in source and `.env`. Reports `JWT_SECRET` and `APP_ENGINE`. |
|
|
24
|
+
| `res-envelope` | report | Locates every `@Res()` handler and manual envelope build, and says which of three ports each one is. |
|
|
25
|
+
| `config-inventory` | report | Inventories every environment variable and config namespace the app reads — the input to `env.schema.ts`. |
|
|
26
|
+
| `job-generics` | report | Locates the `any`s that defeated the legacy `Job` / `ModelService` generics. |
|
|
27
|
+
|
|
28
|
+
A **rewrite** edits the file, and only where the result is what a careful
|
|
29
|
+
developer would have typed: the two rewritten specifiers move name for name onto
|
|
30
|
+
successors this repository ships, and the rewrite is skipped entirely if any name
|
|
31
|
+
on the import has no successor. A **report** never edits anything; it prints
|
|
32
|
+
`path:line`, what is wrong there and what to do, because the decision needs a
|
|
33
|
+
fact the source does not carry.
|
|
34
|
+
|
|
35
|
+
`runCodemods` enforces that: a codemod declared as a report that returns a file
|
|
36
|
+
change throws rather than writing it.
|
|
37
|
+
|
|
38
|
+
## Running them
|
|
39
|
+
|
|
40
|
+
```text
|
|
41
|
+
npx nage-codemod ../nest-core-v2 # report only; nothing is written
|
|
42
|
+
npx nage-codemod ../nest-core-v2 --write # apply the rewrites
|
|
43
|
+
npx nage-codemod ../nest-core-v2 --only import-paths --write
|
|
44
|
+
npx nage-codemod --list
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Reporting is the default and `--write` is opt-in, which is the inverse of most
|
|
48
|
+
codemod runners. Three of the five produce nothing but notes, so the useful first
|
|
49
|
+
run is a read-only one.
|
|
50
|
+
|
|
51
|
+
The same thing programmatically, for a runner of your own:
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { runCodemods } from '@nage-api/compat';
|
|
55
|
+
|
|
56
|
+
const report = runCodemods({
|
|
57
|
+
files: [
|
|
58
|
+
{
|
|
59
|
+
path: 'src/order.controller.ts',
|
|
60
|
+
content: "import { Result } from '../../core/core.responses';\n",
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// `changed` lists the paths whose content differs; `files` carries every file,
|
|
66
|
+
// edited or not, so a caller can write the whole set back or diff it.
|
|
67
|
+
export const rewritten: readonly string[] = report.changed;
|
|
68
|
+
export const toRead: number = report.notes.length;
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Serving the old envelope, one route at a time
|
|
72
|
+
|
|
73
|
+
`NageCompatModule.forRoot()` changes nothing on its own — the interceptor it
|
|
74
|
+
registers only acts on routes marked `@LegacyEnvelope()`:
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
import { Module } from '@nestjs/common';
|
|
78
|
+
import { NageCompatModule } from '@nage-api/compat';
|
|
79
|
+
|
|
80
|
+
@Module({ imports: [NageCompatModule.forRoot()] })
|
|
81
|
+
export class AppModule {}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import { Controller, Get } from '@nestjs/common';
|
|
86
|
+
import { LegacyEnvelope } from '@nage-api/compat';
|
|
87
|
+
import type { Paginated } from '@nage-api/contracts';
|
|
88
|
+
|
|
89
|
+
interface Order {
|
|
90
|
+
readonly id: number;
|
|
91
|
+
readonly reference: string;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
declare const orders: { findAll(): Promise<Paginated<Order>> };
|
|
95
|
+
|
|
96
|
+
@Controller('orders')
|
|
97
|
+
export class OrderController {
|
|
98
|
+
/** Ported: returns data, never touches `res`. Old clients still read `records`. */
|
|
99
|
+
@Get()
|
|
100
|
+
@LegacyEnvelope()
|
|
101
|
+
findAll(): Promise<Paginated<Order>> {
|
|
102
|
+
return orders.findAll();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
That route answers `{ records, offset, limit, count, timestamp }` — the counters
|
|
108
|
+
back at the top level, undoing the `meta.pagination` nesting §16.1 introduced.
|
|
109
|
+
Remove the decorator when the clients have moved and the route rejoins the
|
|
110
|
+
standard envelope.
|
|
111
|
+
|
|
112
|
+
**The flag is per route, not per application.** `NageCoreModule` registers
|
|
113
|
+
`ResponseInterceptor` unconditionally, so a global switch would have to win a
|
|
114
|
+
race with it decided by module import order — a correctness property invisible at
|
|
115
|
+
the call site. `@LegacyEnvelope()` instead rides on `@NoEnvelope()`, which makes
|
|
116
|
+
the core interceptor stand down for exactly these routes whatever order the
|
|
117
|
+
modules were imported in. It also matches how §23.3 phase 5 says to migrate:
|
|
118
|
+
controller by controller.
|
|
119
|
+
|
|
120
|
+
## The legacy body is reconstructed, so check it
|
|
121
|
+
|
|
122
|
+
`nest-core-v2` is not a dependency of this repository and its
|
|
123
|
+
`src/core/core.responses.ts` is not readable from here. The default body is
|
|
124
|
+
rebuilt from the two call sites PLAN.md quotes — `Result(res, { data, message })`
|
|
125
|
+
(§4.1) and `Result(res, { records, offset, limit, count })` (§22) — plus §16.1's
|
|
126
|
+
note that the old envelope stamped `timestamp` on 200s and not on errors. So it
|
|
127
|
+
spreads the payload and stamps a timestamp, which is the only behaviour both
|
|
128
|
+
quoted call sites agree on.
|
|
129
|
+
|
|
130
|
+
Diff it against your own and, where it differs, supply the builder rather than
|
|
131
|
+
patching this one:
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
import { NageCompatModule, type LegacyBodyBuilder } from '@nage-api/compat';
|
|
135
|
+
import { Module } from '@nestjs/common';
|
|
136
|
+
|
|
137
|
+
const ourBody: LegacyBodyBuilder = ({ payload, timestamp }) => ({
|
|
138
|
+
status: 'success',
|
|
139
|
+
result: payload,
|
|
140
|
+
timestamp,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
@Module({ imports: [NageCompatModule.forRoot({ body: ourBody })] })
|
|
144
|
+
export class AppModule {}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
One thing is deliberately not reproduced: the legacy `ErrorResponse` echoed
|
|
148
|
+
`error.message || error` to the client, which is how SQL text and upstream
|
|
149
|
+
response bodies reached callers (§5.2). `legacyErrorBody` takes an already
|
|
150
|
+
sanitised `ErrorPayload`, so the shape comes back and the leak does not.
|
|
151
|
+
|
|
152
|
+
## `Result` and `Created` for controllers that still hold `@Res()`
|
|
153
|
+
|
|
154
|
+
The `import-paths` codemod repoints `…/core.responses` here, which makes a
|
|
155
|
+
legacy controller compile and behave inside a `@nage-api` application before anyone
|
|
156
|
+
touches its signature:
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
import { Controller, Get, Res } from '@nestjs/common';
|
|
160
|
+
import { Result, type LegacyResponse } from '@nage-api/compat';
|
|
161
|
+
|
|
162
|
+
@Controller('orders')
|
|
163
|
+
export class OrderController {
|
|
164
|
+
@Get()
|
|
165
|
+
findAll(@Res() res: LegacyResponse): unknown {
|
|
166
|
+
return Result(res, { records: [], offset: 0, limit: 10, count: 0 });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
This is a step, not a destination. A handler with `@Res()` in its signature is in
|
|
172
|
+
library-specific mode: Nest ignores what it returns, `ResponseInterceptor` never
|
|
173
|
+
sees the value and `AllExceptionsFilter` never sees its errors. That is the defect
|
|
174
|
+
§16.1 replaced, and `res-envelope` lists every site of it.
|
|
175
|
+
|
|
176
|
+
The response is typed as `LegacyResponse` — two methods — rather than Express's
|
|
177
|
+
`Response`, so this package pulls no HTTP framework into an application running
|
|
178
|
+
Fastify, and a test can pass an object literal.
|
|
179
|
+
|
|
180
|
+
## What this deliberately does not do
|
|
181
|
+
|
|
182
|
+
- **It does not repeat `nage doctor --legacy`.** That scan finds the insecure
|
|
183
|
+
literals the legacy framework shipped — `rejectUnauthorized: false`,
|
|
184
|
+
`enableCors()`, `Math.random(`, `synchronize: true`, the seeded credentials.
|
|
185
|
+
Run it first; nothing here duplicates it.
|
|
186
|
+
- **It does not rewrite `Result(res, x)` to `return x`.** Rewriting the body
|
|
187
|
+
without removing the `@Res()` parameter produces a route that hangs, and
|
|
188
|
+
removing a parameter changes the signature, its callers and its tests — none of
|
|
189
|
+
which is decidable from the line being rewritten. `res-envelope` reports each
|
|
190
|
+
site with which of the three shapes it is.
|
|
191
|
+
- **It does not repoint `…/core.errors`.** `NotFoundError` and `ValidationError`
|
|
192
|
+
exist in `@nage-api/core` under the same names, so that rewrite would compile — and
|
|
193
|
+
change the status code and the stable `error.code` the client receives. A
|
|
194
|
+
rewrite whose failure mode is a passing build is the one not to make.
|
|
195
|
+
- **It ships no entity/schema introspection codemod**, though §23.4 names one.
|
|
196
|
+
Its input is a live database's `information_schema` rather than source; its
|
|
197
|
+
output is a migration in a format `nage db migrate` cannot yet apply; and
|
|
198
|
+
nothing here could test it against a real schema. Written against a guess it
|
|
199
|
+
would be the most dangerous artefact in the set — a plausible schema migration
|
|
200
|
+
for a production database.
|
|
201
|
+
|
|
202
|
+
## Not yet implemented
|
|
203
|
+
|
|
204
|
+
- **Errors on a `@LegacyEnvelope()` route still get the new error envelope.**
|
|
205
|
+
`AllExceptionsFilter` is global and this package does not replace it. Restoring
|
|
206
|
+
the old error body would mean a second filter carrying its own copy of the
|
|
207
|
+
status mapping, and the old shape is the one that leaked.
|
|
208
|
+
- **`@NoEnvelope()` and `@LegacyEnvelope()` cannot both be meaningful on one
|
|
209
|
+
route**; the second is built from the first.
|
|
210
|
+
- **None of this has been validated against a real `nest-core-v2` application.**
|
|
211
|
+
§28.10 asks for the migration guide to be proved by porting one end to end, and
|
|
212
|
+
no such application is reachable from this repository. The codemods are tested
|
|
213
|
+
against before/after fixtures written from PLAN.md's evidence, which proves they
|
|
214
|
+
do what they say — not that what they say covers a real codebase. Treat the
|
|
215
|
+
first run on a real repository as the review it is.
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
/**
|
|
4
|
+
* The `nage-codemod` executable.
|
|
5
|
+
*
|
|
6
|
+
* The only file in this package that reads `process` or touches disk; everything
|
|
7
|
+
* it calls is a function a test invokes directly.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
const node_fs_js_1 = require("./codemods/node-fs.js");
|
|
11
|
+
const cli_main_js_1 = require("./codemods/cli-main.js");
|
|
12
|
+
(0, cli_main_js_1.runCodemodCli)({
|
|
13
|
+
argv: process.argv.slice(2),
|
|
14
|
+
cwd: process.cwd(),
|
|
15
|
+
io: {
|
|
16
|
+
out: (message) => void process.stdout.write(message),
|
|
17
|
+
err: (message) => void process.stderr.write(message),
|
|
18
|
+
},
|
|
19
|
+
fs: (0, node_fs_js_1.nodeCodemodFs)(),
|
|
20
|
+
})
|
|
21
|
+
.then((code) => {
|
|
22
|
+
// `exitCode` rather than `exit()`: piped output is truncated otherwise.
|
|
23
|
+
process.exitCode = code;
|
|
24
|
+
})
|
|
25
|
+
.catch((error) => {
|
|
26
|
+
process.stderr.write(`✖ ${error instanceof Error ? (error.stack ?? error.message) : String(error)}\n`);
|
|
27
|
+
process.exitCode = 1;
|
|
28
|
+
});
|
|
29
|
+
//# sourceMappingURL=cli.js.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `nage-codemod` — the command layer (PLAN.md §23.4).
|
|
3
|
+
*
|
|
4
|
+
* Everything here is a pure function of its arguments and two injected ports, so
|
|
5
|
+
* the whole command is testable without a filesystem or a process. The bin
|
|
6
|
+
* (`src/cli.ts`) is the only file that reads `process` or touches disk.
|
|
7
|
+
*
|
|
8
|
+
* Reporting is the default and `--write` is opt-in, which is the inverse of most
|
|
9
|
+
* codemod runners. The reason is the shape of this tool: three of its five
|
|
10
|
+
* codemods produce nothing but notes, so the useful first run is a read-only
|
|
11
|
+
* one, and a developer who has to ask for the edits has already decided to look
|
|
12
|
+
* at them.
|
|
13
|
+
*/
|
|
14
|
+
export interface CodemodIo {
|
|
15
|
+
out(message: string): void;
|
|
16
|
+
err(message: string): void;
|
|
17
|
+
}
|
|
18
|
+
/** The filesystem operations the runner needs, so tests can supply a fake. */
|
|
19
|
+
export interface CodemodFileSystem {
|
|
20
|
+
/** Every candidate file under `root`, as paths `read`/`write` accept. */
|
|
21
|
+
list(root: string): Promise<readonly string[]>;
|
|
22
|
+
read(path: string): Promise<string>;
|
|
23
|
+
write(path: string, content: string): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
export interface CodemodCliOptions {
|
|
26
|
+
readonly argv: readonly string[];
|
|
27
|
+
readonly cwd: string;
|
|
28
|
+
readonly io: CodemodIo;
|
|
29
|
+
readonly fs: CodemodFileSystem;
|
|
30
|
+
}
|
|
31
|
+
export declare function runCodemodCli(options: CodemodCliOptions): Promise<number>;
|
|
32
|
+
//# sourceMappingURL=cli-main.d.ts.map
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* `nage-codemod` — the command layer (PLAN.md §23.4).
|
|
4
|
+
*
|
|
5
|
+
* Everything here is a pure function of its arguments and two injected ports, so
|
|
6
|
+
* the whole command is testable without a filesystem or a process. The bin
|
|
7
|
+
* (`src/cli.ts`) is the only file that reads `process` or touches disk.
|
|
8
|
+
*
|
|
9
|
+
* Reporting is the default and `--write` is opt-in, which is the inverse of most
|
|
10
|
+
* codemod runners. The reason is the shape of this tool: three of its five
|
|
11
|
+
* codemods produce nothing but notes, so the useful first run is a read-only
|
|
12
|
+
* one, and a developer who has to ask for the edits has already decided to look
|
|
13
|
+
* at them.
|
|
14
|
+
*/
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.runCodemodCli = runCodemodCli;
|
|
17
|
+
const node_util_1 = require("node:util");
|
|
18
|
+
const registry_js_1 = require("./registry.js");
|
|
19
|
+
const runner_js_1 = require("./runner.js");
|
|
20
|
+
const types_js_1 = require("./types.js");
|
|
21
|
+
const USAGE = `nage-codemod — migration codemods for nest-core-v2 → @nage-api (PLAN.md §23.4)
|
|
22
|
+
|
|
23
|
+
nage-codemod <path> [options]
|
|
24
|
+
|
|
25
|
+
Options:
|
|
26
|
+
--only <names> comma-separated codemod names (default: all)
|
|
27
|
+
--write apply the rewrites; without it nothing is written
|
|
28
|
+
--list list the codemods and what each one is
|
|
29
|
+
-h, --help this text
|
|
30
|
+
|
|
31
|
+
A "rewrite" codemod edits files. A "report" codemod never does: it prints the
|
|
32
|
+
places a change is needed and what the change is, because the decision needs a
|
|
33
|
+
fact the source does not carry. Trust the rewrites; read the reports.
|
|
34
|
+
`;
|
|
35
|
+
function listCodemods(io) {
|
|
36
|
+
io.out('\n');
|
|
37
|
+
for (const codemod of registry_js_1.CODEMODS) {
|
|
38
|
+
io.out(` ${codemod.kind.padEnd(8)} ${codemod.name.padEnd(18)} ${codemod.section}\n`);
|
|
39
|
+
io.out(` ${' '.repeat(8)} ${codemod.summary}\n\n`);
|
|
40
|
+
}
|
|
41
|
+
return 0;
|
|
42
|
+
}
|
|
43
|
+
/** `.env` files carry variable names too, so they are read alongside source. */
|
|
44
|
+
function isCandidate(path) {
|
|
45
|
+
const name = path.split(/[/\\]/).pop() ?? '';
|
|
46
|
+
return (0, types_js_1.isSourceFile)(path) || name === '.env' || name.startsWith('.env.');
|
|
47
|
+
}
|
|
48
|
+
async function runCodemodCli(options) {
|
|
49
|
+
const { io } = options;
|
|
50
|
+
let parsed;
|
|
51
|
+
try {
|
|
52
|
+
parsed = (0, node_util_1.parseArgs)({
|
|
53
|
+
args: [...options.argv],
|
|
54
|
+
options: {
|
|
55
|
+
only: { type: 'string' },
|
|
56
|
+
write: { type: 'boolean' },
|
|
57
|
+
list: { type: 'boolean' },
|
|
58
|
+
help: { type: 'boolean', short: 'h' },
|
|
59
|
+
},
|
|
60
|
+
allowPositionals: true,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
io.err(`✖ ${error instanceof Error ? error.message : String(error)}\n`);
|
|
65
|
+
return 1;
|
|
66
|
+
}
|
|
67
|
+
if (parsed.values.help === true) {
|
|
68
|
+
io.out(USAGE);
|
|
69
|
+
return 0;
|
|
70
|
+
}
|
|
71
|
+
if (parsed.values.list === true)
|
|
72
|
+
return listCodemods(io);
|
|
73
|
+
const root = parsed.positionals[0];
|
|
74
|
+
if (root === undefined) {
|
|
75
|
+
io.err('✖ a path to scan is required.\n\n');
|
|
76
|
+
io.err(USAGE);
|
|
77
|
+
return 1;
|
|
78
|
+
}
|
|
79
|
+
let only;
|
|
80
|
+
if (parsed.values.only !== undefined) {
|
|
81
|
+
only = parsed.values.only.split(',').map((name) => name.trim());
|
|
82
|
+
const unknown = only.filter((name) => (0, registry_js_1.findCodemod)(name) === undefined);
|
|
83
|
+
if (unknown.length > 0) {
|
|
84
|
+
io.err(`✖ unknown codemod(s): ${unknown.join(', ')}\n`);
|
|
85
|
+
io.err(` known: ${registry_js_1.CODEMODS.map((codemod) => codemod.name).join(', ')}\n`);
|
|
86
|
+
return 1;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const paths = (await options.fs.list(root)).filter(isCandidate);
|
|
90
|
+
const files = [];
|
|
91
|
+
for (const path of paths)
|
|
92
|
+
files.push({ path, content: await options.fs.read(path) });
|
|
93
|
+
const report = (0, runner_js_1.runCodemods)({ files, ...(only === undefined ? {} : { only }) });
|
|
94
|
+
io.out(`\nScanned ${String(files.length)} file(s) under ${root}\n`);
|
|
95
|
+
for (const codemod of only === undefined
|
|
96
|
+
? registry_js_1.CODEMODS
|
|
97
|
+
: registry_js_1.CODEMODS.filter((c) => only.includes(c.name))) {
|
|
98
|
+
const edits = report.edits.filter((edit) => edit.codemod === codemod.name);
|
|
99
|
+
const notes = report.notes.filter((note) => note.codemod === codemod.name);
|
|
100
|
+
if (edits.length === 0 && notes.length === 0)
|
|
101
|
+
continue;
|
|
102
|
+
io.out(`\n── ${codemod.name} (${codemod.kind}, ${codemod.section}) ──\n`);
|
|
103
|
+
for (const edit of edits) {
|
|
104
|
+
io.out(`\n ${edit.path}:${String(edit.line)}\n`);
|
|
105
|
+
io.out(` - ${edit.before}\n`);
|
|
106
|
+
io.out(` + ${edit.after}\n`);
|
|
107
|
+
}
|
|
108
|
+
for (const note of notes) {
|
|
109
|
+
io.out(`\n ${note.path}:${String(note.line)} [${note.rule}]\n`);
|
|
110
|
+
io.out(` ${note.message}\n`);
|
|
111
|
+
io.out(` → ${note.action}\n`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (parsed.values.write === true) {
|
|
115
|
+
for (const path of report.changed) {
|
|
116
|
+
const file = report.files.find((candidate) => candidate.path === path);
|
|
117
|
+
if (file !== undefined)
|
|
118
|
+
await options.fs.write(path, file.content);
|
|
119
|
+
}
|
|
120
|
+
io.out(`\n${String(report.changed.length)} file(s) rewritten.\n`);
|
|
121
|
+
}
|
|
122
|
+
else if (report.changed.length > 0) {
|
|
123
|
+
io.out(`\n${String(report.changed.length)} file(s) would be rewritten — nothing was written. Pass --write to apply.\n`);
|
|
124
|
+
}
|
|
125
|
+
io.out(`${String(report.notes.length)} note(s) to read.\n`);
|
|
126
|
+
return 0;
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=cli-main.js.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Config blob → `defineConfig` (PLAN.md §23.4) — as a report.
|
|
3
|
+
*
|
|
4
|
+
* `docs/migration.md` already reached this conclusion in prose: "mapping your
|
|
5
|
+
* old configuration onto this is the least mechanical part of the whole
|
|
6
|
+
* migration, and it is worth doing by hand rather than transliterating". The
|
|
7
|
+
* reason is that the legacy shape carries less information than the new one
|
|
8
|
+
* needs. `env.get<string>('DATABASE_URL')` is a cast, not a check, so the source
|
|
9
|
+
* says nothing about whether the value is required, what it must look like, or
|
|
10
|
+
* what happens when it is absent — and those three answers *are* the zod schema.
|
|
11
|
+
*
|
|
12
|
+
* What a machine can do is the tedious half: find every variable the application
|
|
13
|
+
* reads, wherever it reads it from, so the schema is written against a list
|
|
14
|
+
* rather than against memory. That is what this produces — one note per distinct
|
|
15
|
+
* variable, at its first use, plus a note at each site where a default is
|
|
16
|
+
* hiding a misconfiguration.
|
|
17
|
+
*/
|
|
18
|
+
import { type Codemod } from './types.js';
|
|
19
|
+
export declare const configInventoryCodemod: Codemod;
|
|
20
|
+
//# sourceMappingURL=config-inventory.d.ts.map
|