frappe-codegen 1.0.0
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/CHANGELOG.md +13 -0
- package/LICENSE +21 -0
- package/README.md +158 -0
- package/dist/cli.js +531 -0
- package/dist/index.d.mts +115 -0
- package/dist/index.d.ts +115 -0
- package/dist/index.js +387 -0
- package/dist/index.mjs +373 -0
- package/package.json +88 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [`d758a5b`](https://github.com/dhiashalabi/frappe-js-client/commit/d758a5b4fa15da2ed8ffe19d3fbc4f36505f3738) Thanks [@dhiashalabi](https://github.com/dhiashalabi)! - Harden the 1.0 client: fail closed on non-2xx middleware responses, unify XHR/fetch error mapping, honor `hasNextPage` in `paginate()`, reject upload progress combined with middleware, throw `ConfigurationError` instead of plain `Error`, default generics to `unknown`, hide `unsafeTransport()`, and export previously leaked types. Codegen reports a clear v2-only meta error and adds golden-file output tests.
|
|
8
|
+
- Updated dependencies [[`d758a5b`](https://github.com/dhiashalabi/frappe-js-client/commit/d758a5b4fa15da2ed8ffe19d3fbc4f36505f3738)]:
|
|
9
|
+
- frappe-js-client@3.3.0
|
|
10
|
+
|
|
11
|
+
All notable changes to `@frappe-js-client/codegen` will be documented in this file.
|
|
12
|
+
|
|
13
|
+
See [changesets](https://github.com/changesets/changesets). Entries are generated on version.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dhia A. Shalabi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# frappe-codegen
|
|
2
|
+
|
|
3
|
+
Reads DocType metadata from a **live Frappe site** and emits typed TypeScript interfaces for
|
|
4
|
+
[`frappe-js-client`](https://www.npmjs.com/package/frappe-js-client), so
|
|
5
|
+
`createFrappeClient<GeneratedDocTypes>(...)` infers `db.getDoc('ToDo', name)` instead of
|
|
6
|
+
`Record<string, unknown>`.
|
|
7
|
+
|
|
8
|
+
Full guide: [Codegen docs](https://dhiashalabi.github.io/frappe-js-client/docs/codegen)
|
|
9
|
+
|
|
10
|
+
This package has its own release cycle. It depends on `frappe-js-client`; the client never depends
|
|
11
|
+
on codegen.
|
|
12
|
+
|
|
13
|
+
Requires **Frappe v15+** (`apiVersion: 2`). Meta comes from `GET /api/v2/doctype/{doctype}/meta`.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm add -D frappe-codegen
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The binary is `frappe-codegen`.
|
|
22
|
+
|
|
23
|
+
## Recommended: config + env
|
|
24
|
+
|
|
25
|
+
Put **non-secrets** in `frappe-codegen.config.json`. Put credentials in the environment. Then run
|
|
26
|
+
the binary with no flags (or a `package.json` script).
|
|
27
|
+
|
|
28
|
+
`frappe-codegen.config.json`:
|
|
29
|
+
|
|
30
|
+
```json
|
|
31
|
+
{
|
|
32
|
+
"url": "https://frappe.example.com",
|
|
33
|
+
"out": "src/generated/frappe-types.ts",
|
|
34
|
+
"includeHidden": false,
|
|
35
|
+
"followTables": true,
|
|
36
|
+
"doctypes": ["ToDo", "User"],
|
|
37
|
+
"modules": ["Desk"]
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
export FRAPPE_URL="https://frappe.example.com" # optional if `url` is in the file
|
|
43
|
+
export FRAPPE_API_KEY="…"
|
|
44
|
+
export FRAPPE_API_SECRET="…"
|
|
45
|
+
|
|
46
|
+
pnpm exec frappe-codegen
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
`package.json`:
|
|
50
|
+
|
|
51
|
+
```json
|
|
52
|
+
{
|
|
53
|
+
"scripts": {
|
|
54
|
+
"codegen": "frappe-codegen"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Then `pnpm codegen`.
|
|
60
|
+
|
|
61
|
+
**Do not** put `apiKey`, `apiSecret`, or `api-key` in the JSON file. The CLI rejects that.
|
|
62
|
+
|
|
63
|
+
### Precedence
|
|
64
|
+
|
|
65
|
+
Flags override env, env overrides the config file.
|
|
66
|
+
|
|
67
|
+
`--doctype` / `--module` are **merged** with `doctypes` / `modules` from the file (union, not
|
|
68
|
+
replace).
|
|
69
|
+
|
|
70
|
+
Config file keys: `url`, `out`, `includeHidden`, `followTables`, `doctypes`, `modules`. Labels,
|
|
71
|
+
the DocType map, and `--dry-run` are CLI-only.
|
|
72
|
+
|
|
73
|
+
### Other ways to run
|
|
74
|
+
|
|
75
|
+
| When | Command |
|
|
76
|
+
| -------------------------------------- | ------------------------------------------------- |
|
|
77
|
+
| Installed in the project (recommended) | `pnpm exec frappe-codegen` |
|
|
78
|
+
| npm / yarn | `npx frappe-codegen` |
|
|
79
|
+
| One-off, no install | `pnpm dlx frappe-codegen` or `npx frappe-codegen` |
|
|
80
|
+
| Help | `pnpm exec frappe-codegen --help` |
|
|
81
|
+
|
|
82
|
+
## One-shot (no config file)
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
pnpm exec frappe-codegen \
|
|
86
|
+
--url https://frappe.example.com \
|
|
87
|
+
--api-key "$FRAPPE_API_KEY" --api-secret "$FRAPPE_API_SECRET" \
|
|
88
|
+
--doctype "ToDo" --doctype "User" \
|
|
89
|
+
--include-hidden \
|
|
90
|
+
--out src/generated/frappe-types.ts
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Omit `--api-key` / `--api-secret` (and the env vars) for **anonymous** meta reads if Guest can
|
|
94
|
+
access DocType metadata.
|
|
95
|
+
|
|
96
|
+
## CLI reference
|
|
97
|
+
|
|
98
|
+
Required after merge: a site URL, and at least one DocType source (`--doctype`, `--module`, or
|
|
99
|
+
config `doctypes` / `modules`).
|
|
100
|
+
|
|
101
|
+
| Flag | Default | Meaning |
|
|
102
|
+
| ---------------------------------------------------- | ------------------------------------------- | ------------------------------------------------ |
|
|
103
|
+
| `-u`, `--url <url>` | `FRAPPE_URL` or config `url` | Frappe site base URL |
|
|
104
|
+
| `-d`, `--doctype <name>` | — | DocType to generate. Repeatable |
|
|
105
|
+
| `--module <name>` | — | All DocTypes in that Frappe module. Repeatable |
|
|
106
|
+
| `--api-key <key>` | `FRAPPE_API_KEY` | API key (token auth) |
|
|
107
|
+
| `--api-secret <secret>` | `FRAPPE_API_SECRET` | API secret |
|
|
108
|
+
| `-o`, `--out <path>` | `./frappe-types.generated.ts` | Output `.ts` file. Parent dirs are created |
|
|
109
|
+
| `--config <path>` | `./frappe-codegen.config.json` if it exists | Config file path |
|
|
110
|
+
| `--follow-tables` / `--no-follow-tables` | follow (on) | Also generate Table / Table MultiSelect children |
|
|
111
|
+
| `--include-hidden` | off | Emit form-hidden fields |
|
|
112
|
+
| `--include-labels` / `--no-include-labels` | labels on | `/** label */` above each field |
|
|
113
|
+
| `--include-doctype-map` / `--no-include-doctype-map` | map on | Emit `GeneratedDocTypes` / `GeneratedInserts` |
|
|
114
|
+
| `--dry-run` | off | Print `name<TAB>seed\|child` and exit (no file) |
|
|
115
|
+
| `-h`, `--help` | — | Print help |
|
|
116
|
+
|
|
117
|
+
Frappe `hidden` is **form visibility**, not “missing on the document”. Reminder’s required `user`
|
|
118
|
+
and `notified` need `--include-hidden`.
|
|
119
|
+
|
|
120
|
+
Virtual fields (`is_virtual`) and layout fieldtypes (`Section Break`, `Image`, `Button`, …) are
|
|
121
|
+
never emitted.
|
|
122
|
+
|
|
123
|
+
`--dry-run` still fetches metadata (including children if follow is on) so you can see seed vs
|
|
124
|
+
child names.
|
|
125
|
+
|
|
126
|
+
## Typed client
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
import { createFrappeClient } from 'frappe-js-client'
|
|
130
|
+
import type { GeneratedDocTypes } from './generated/frappe-types'
|
|
131
|
+
|
|
132
|
+
const frappe = createFrappeClient<GeneratedDocTypes>({ url, auth })
|
|
133
|
+
const todo = await frappe.db.getDoc('ToDo', name)
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## What gets generated
|
|
137
|
+
|
|
138
|
+
Each DocType becomes a read type (`FrappeDoc<{ doctype: "…", … }>`) plus `SomethingInsert`
|
|
139
|
+
(`FrappeInsert<…>`). Check fields are required on the read type (`0 | 1`) and optional on insert.
|
|
140
|
+
`GeneratedDocTypes` / `GeneratedInserts` map Frappe names to those types.
|
|
141
|
+
|
|
142
|
+
Link fields with a target become `Link<"Customer">`. Table children are generated when follow is
|
|
143
|
+
on (default) or when you list the child DocType yourself.
|
|
144
|
+
|
|
145
|
+
## Programmatic API
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
import { createFrappeClient } from 'frappe-js-client'
|
|
149
|
+
import { fetchWithOptionalFollow, generateModule, loadConfigFile, mergeConfig, resolveDocTypes } from 'frappe-codegen'
|
|
150
|
+
|
|
151
|
+
const client = createFrappeClient({ url, apiVersion: 2, auth })
|
|
152
|
+
const names = await resolveDocTypes(client, {
|
|
153
|
+
doctypes: ['ToDo'],
|
|
154
|
+
modules: [],
|
|
155
|
+
})
|
|
156
|
+
const metas = await fetchWithOptionalFollow(client, names, true)
|
|
157
|
+
const source = generateModule(metas, { includeHidden: true })
|
|
158
|
+
```
|