@tryghost/mg-context 0.1.0-alpha.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/LICENSE +21 -0
- package/README.md +260 -0
- package/build/index.d.ts +7 -0
- package/build/index.js +8 -0
- package/build/index.js.map +1 -0
- package/build/lib/AuthorContext.d.ts +25 -0
- package/build/lib/AuthorContext.js +43 -0
- package/build/lib/AuthorContext.js.map +1 -0
- package/build/lib/MigrateBase.d.ts +12 -0
- package/build/lib/MigrateBase.js +117 -0
- package/build/lib/MigrateBase.js.map +1 -0
- package/build/lib/MigrateContext.d.ts +35 -0
- package/build/lib/MigrateContext.js +165 -0
- package/build/lib/MigrateContext.js.map +1 -0
- package/build/lib/PostContext.d.ts +65 -0
- package/build/lib/PostContext.js +165 -0
- package/build/lib/PostContext.js.map +1 -0
- package/build/lib/TagContext.d.ts +26 -0
- package/build/lib/TagContext.js +48 -0
- package/build/lib/TagContext.js.map +1 -0
- package/build/test/author-context.test.d.ts +1 -0
- package/build/test/author-context.test.js +79 -0
- package/build/test/author-context.test.js.map +1 -0
- package/build/test/lib/NoErrorThrownError.d.ts +4 -0
- package/build/test/lib/NoErrorThrownError.js +13 -0
- package/build/test/lib/NoErrorThrownError.js.map +1 -0
- package/build/test/migrate-base.test.d.ts +1 -0
- package/build/test/migrate-base.test.js +169 -0
- package/build/test/migrate-base.test.js.map +1 -0
- package/build/test/migrate-context.test.d.ts +1 -0
- package/build/test/migrate-context.test.js +330 -0
- package/build/test/migrate-context.test.js.map +1 -0
- package/build/test/post-context.test.d.ts +1 -0
- package/build/test/post-context.test.js +384 -0
- package/build/test/post-context.test.js.map +1 -0
- package/build/test/tag-context.test.d.ts +1 -0
- package/build/test/tag-context.test.js +80 -0
- package/build/test/tag-context.test.js.map +1 -0
- package/build/test/tasks.test.d.ts +1 -0
- package/build/test/tasks.test.js +85 -0
- package/build/test/tasks.test.js.map +1 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013-2023 Ghost Foundation
|
|
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,260 @@
|
|
|
1
|
+
# Migrate Context
|
|
2
|
+
|
|
3
|
+
This package makes it easier to create post objects that can be used in the Ghost migration tools. It aims to provide a consistent, typed interface for posts, pages, tags, and authors. It also validates input data to ensure that the resulting object is valid for importing into Ghost.
|
|
4
|
+
|
|
5
|
+
It can also be used for custom migrations, and can output a JSON file that can be imported in Ghost.
|
|
6
|
+
|
|
7
|
+
This package exports a few different classes: `MigrateContext`, `PostContext`, `TagContext`, and `AuthorContext`.
|
|
8
|
+
Most of the time, you'll want the `MigrateContext` class, which is the top-level class that manages all the other classes.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
`npm install @tryghost/mg-context --save`
|
|
13
|
+
|
|
14
|
+
or
|
|
15
|
+
|
|
16
|
+
`yarn add @tryghost/mg-context`
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# Usage
|
|
20
|
+
|
|
21
|
+
Let's walk through adding a post to a context, and then outputting the final JSON that can be imported into Ghost.
|
|
22
|
+
|
|
23
|
+
### Create a context
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
import {MigrateContext} from '@tryghost/mg-context';
|
|
27
|
+
const context = new MigrateContext();
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Create a post
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
const post = context.addPost();
|
|
34
|
+
post.set('title', 'My Post');
|
|
35
|
+
post.set('slug', 'my-post');
|
|
36
|
+
post.set('status', 'published');
|
|
37
|
+
post.set('published_at', new Date('2023-12-08T13:34:22.000Z'));
|
|
38
|
+
post.set('created_at', new Date('2023-12-08T13:23:03.000Z'));
|
|
39
|
+
post.set('updated_at', new Date('2023-12-08T13:36:42.000Z'));
|
|
40
|
+
post.set('html', '<p>My post content</p>');
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Add a tag
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
post.addTag({
|
|
47
|
+
name: 'My Tag',
|
|
48
|
+
slug: 'my-tag'
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Add an author
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
post.addAuthor({
|
|
56
|
+
name: 'Author Name',
|
|
57
|
+
slug: 'author-name',
|
|
58
|
+
email: 'name@example.com'
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Adding posts from another source
|
|
63
|
+
|
|
64
|
+
If you have post data in another format, get it in to some sort of array or object that can be iterated over. Then, for each row, create a new post and set the data with the same methods as above.
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
const arrayOfPostData = [...];
|
|
68
|
+
|
|
69
|
+
arrayOfPostData.forEach((row) => {
|
|
70
|
+
const post = context.addPost();
|
|
71
|
+
// post.set('title', row.title);
|
|
72
|
+
// ...
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
### Get a Ghost JSON string
|
|
78
|
+
|
|
79
|
+
```js
|
|
80
|
+
const json = context.ghostJson;
|
|
81
|
+
// `json` is a JSON string. Save it to a `.json` file and import it into Ghost
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
# Classes & Methods
|
|
89
|
+
|
|
90
|
+
There's 4 main classes: `MigrateContext`, `PostContext`, `TagContext`, and `AuthorContext`. Most of the time, `MigrateContext` is what you'll use, but more advanced usage may require the usage of the other classes.
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
### `MigrateContext`
|
|
94
|
+
|
|
95
|
+
This is the main class that manages all the other classes. It has methods for adding posts, tags, and authors, and also has methods for finding posts, tags, and authors.
|
|
96
|
+
|
|
97
|
+
- `context.addPost(post`
|
|
98
|
+
- `context.findPosts({slug, title, sourceAttr, tagSlug, tagName, authorSlug, authorName, authorEmail})`
|
|
99
|
+
- `context.findTags({slug, name})`
|
|
100
|
+
- `context.findAuthors({slug, name, email})`
|
|
101
|
+
- `await context.forEachPost(callback)`
|
|
102
|
+
- `context.forEachPostSync(callback)`
|
|
103
|
+
- `context.allPosts`
|
|
104
|
+
- `context.ghostJson`
|
|
105
|
+
|
|
106
|
+
### `PostContext`
|
|
107
|
+
|
|
108
|
+
This class is only for posts and related data. You can also set tags and authors.
|
|
109
|
+
|
|
110
|
+
- `post.get(prop)`
|
|
111
|
+
- `post.set(prop)`
|
|
112
|
+
- `post.remove(prop)`
|
|
113
|
+
- `post.setMeta(value)`
|
|
114
|
+
- `post.getMetaValue(key)`
|
|
115
|
+
- `post.getSourceValue(key)`
|
|
116
|
+
- `post.hasTagSlug(tagSlug)`
|
|
117
|
+
- `post.hasTagName(tagName)`
|
|
118
|
+
- `post.addTag(value)`
|
|
119
|
+
- `post.removeTag(tagSlug)`
|
|
120
|
+
- `post.setTagOrder(callback)`
|
|
121
|
+
- `post.setPrimaryTag(value)`
|
|
122
|
+
- `post.hasAuthorSlug(authorSlug)`
|
|
123
|
+
- `post.hasAuthorName(authorName)`
|
|
124
|
+
- `post.hasAuthorEmail(authorEmail)`
|
|
125
|
+
- `post.addAuthor(value)`
|
|
126
|
+
- `post.removeAuthor(authorSlug)`
|
|
127
|
+
- `post.setAuthorOrder(callback)`
|
|
128
|
+
- `post.setPrimaryAuthor(value)`
|
|
129
|
+
- `post.meta`
|
|
130
|
+
- `post.source`
|
|
131
|
+
|
|
132
|
+
### `TagContext`
|
|
133
|
+
|
|
134
|
+
This class is used to create and manage individual tags.
|
|
135
|
+
|
|
136
|
+
- `tag.set(prop)`
|
|
137
|
+
- `tag.remove(prop)`
|
|
138
|
+
- `tag.get(prop)`
|
|
139
|
+
- `tag.meta`
|
|
140
|
+
|
|
141
|
+
### `AuthorContext`
|
|
142
|
+
|
|
143
|
+
This class is used to create and manage individual authors.
|
|
144
|
+
|
|
145
|
+
- `author.set(prop)`
|
|
146
|
+
- `author.remove(prop)`
|
|
147
|
+
- `author.get(prop)`
|
|
148
|
+
- `author.meta`
|
|
149
|
+
|
|
150
|
+
# Tips & tricks
|
|
151
|
+
|
|
152
|
+
Knowing the methods is not that useful in itself. Here's some tips and tricks that are the heart of this package.
|
|
153
|
+
|
|
154
|
+
## Finding Posts
|
|
155
|
+
|
|
156
|
+
If you need to find posts that have a specific tag or author, use the `findPosts` method. You can supply an object with any of the following keys:
|
|
157
|
+
|
|
158
|
+
- `tagSlug`
|
|
159
|
+
- `tagName`
|
|
160
|
+
- `authorSlug`
|
|
161
|
+
- `authorName`
|
|
162
|
+
- `authorEmail`
|
|
163
|
+
|
|
164
|
+
```js
|
|
165
|
+
const context = new MigrateContext();
|
|
166
|
+
|
|
167
|
+
const posts = context.findPosts({tagSlug: 'my-tag'});
|
|
168
|
+
const posts = context.findPosts({tagName: 'My Tag'});
|
|
169
|
+
|
|
170
|
+
const posts = context.findPosts({authorSlug: 'author-name'});
|
|
171
|
+
const posts = context.findPosts({authorName: 'Author Name'});
|
|
172
|
+
const posts = context.findPosts({authorEmail: 'author@example.com'});
|
|
173
|
+
|
|
174
|
+
const posts = context.findPosts({slug: 'my-post'});
|
|
175
|
+
const posts = context.findPosts({title: 'My Post'});
|
|
176
|
+
const posts = context.findPosts({sourceAttr: {
|
|
177
|
+
key: 'url',
|
|
178
|
+
value: 'https://example.com/blog/2023/11/26/original-slug/'
|
|
179
|
+
}});
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### Updating found posts
|
|
183
|
+
|
|
184
|
+
Add a new tag to all posts with a specific tag
|
|
185
|
+
|
|
186
|
+
```js
|
|
187
|
+
const posts = context.findPosts({tagSlug: 'my-tag'});
|
|
188
|
+
|
|
189
|
+
posts.forEach((post) => {
|
|
190
|
+
post.addTag({
|
|
191
|
+
name: 'My New Tag',
|
|
192
|
+
slug: 'my-new-tag'
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Updating found authors
|
|
198
|
+
|
|
199
|
+
Change the email for found authors
|
|
200
|
+
|
|
201
|
+
```js
|
|
202
|
+
const authors = context.findAuthors({slug: 'author-name'});
|
|
203
|
+
|
|
204
|
+
authors.forEach((author) => {
|
|
205
|
+
author.set('email', 'real-email@exmapl.com');
|
|
206
|
+
});
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## Updating found tags
|
|
210
|
+
|
|
211
|
+
Change the name of found tags
|
|
212
|
+
|
|
213
|
+
```js
|
|
214
|
+
const tags = context.findTags({slug: 'my-tag'});
|
|
215
|
+
|
|
216
|
+
tags.forEach((tag) => {
|
|
217
|
+
tag.set('name', 'My New Tag');
|
|
218
|
+
});
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
### Class Flow
|
|
223
|
+
|
|
224
|
+
```
|
|
225
|
+
┌────────────────┐
|
|
226
|
+
│ MigrateContext │
|
|
227
|
+
└───┬────────────┘
|
|
228
|
+
┌▼─────────────────────────────┐
|
|
229
|
+
│ PostContext │
|
|
230
|
+
└┬─────────────────────────────┘
|
|
231
|
+
└─────┬───────────────┐
|
|
232
|
+
┌──────▼─────┐ ┌───────▼───────┐
|
|
233
|
+
│ TagContext │ │ AuthorContext │
|
|
234
|
+
└────────────┘ └───────────────┘
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
## Develop
|
|
239
|
+
|
|
240
|
+
This is a mono repository, managed with [lerna](https://lernajs.io/).
|
|
241
|
+
|
|
242
|
+
Follow the instructions for the top-level repo.
|
|
243
|
+
1. `git clone` this repo & `cd` into it as usual
|
|
244
|
+
2. Run `yarn` to install top-level dependencies.
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
## Run
|
|
248
|
+
|
|
249
|
+
- `yarn dev`
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
## Test
|
|
253
|
+
|
|
254
|
+
- `yarn lint` run just eslint
|
|
255
|
+
- `yarn test` run lint and tests
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
# Copyright & License
|
|
259
|
+
|
|
260
|
+
Copyright (c) 2013-2023 Ghost Foundation - Released under the [MIT license](LICENSE).
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import MigrateBase from './lib/MigrateBase.js';
|
|
2
|
+
import MigrateContext from './lib/MigrateContext.js';
|
|
3
|
+
import PostContext from './lib/PostContext.js';
|
|
4
|
+
import TagContext from './lib/TagContext.js';
|
|
5
|
+
import AuthorContext from './lib/AuthorContext.js';
|
|
6
|
+
export default MigrateContext;
|
|
7
|
+
export { MigrateBase, MigrateContext, PostContext, TagContext, AuthorContext };
|
package/build/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import MigrateBase from './lib/MigrateBase.js';
|
|
2
|
+
import MigrateContext from './lib/MigrateContext.js';
|
|
3
|
+
import PostContext from './lib/PostContext.js';
|
|
4
|
+
import TagContext from './lib/TagContext.js';
|
|
5
|
+
import AuthorContext from './lib/AuthorContext.js';
|
|
6
|
+
export default MigrateContext;
|
|
7
|
+
export { MigrateBase, MigrateContext, PostContext, TagContext, AuthorContext };
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,MAAM,sBAAsB,CAAC;AAC/C,OAAO,cAAc,MAAM,yBAAyB,CAAC;AACrD,OAAO,WAAW,MAAM,sBAAsB,CAAC;AAC/C,OAAO,UAAU,MAAM,qBAAqB,CAAC;AAC7C,OAAO,aAAa,MAAM,wBAAwB,CAAC;AAEnD,eAAe,cAAc,CAAC;AAE9B,OAAO,EACH,WAAW,EACX,cAAc,EACd,WAAW,EACX,UAAU,EACV,aAAa,EAChB,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import MigrateBase from './MigrateBase.js';
|
|
2
|
+
export type AuthorObject = {
|
|
3
|
+
name: string;
|
|
4
|
+
slug: string;
|
|
5
|
+
email?: string;
|
|
6
|
+
profile_image?: string;
|
|
7
|
+
cover_image?: string;
|
|
8
|
+
bio?: string;
|
|
9
|
+
website?: string;
|
|
10
|
+
location?: string;
|
|
11
|
+
facebook?: string;
|
|
12
|
+
twitter?: string;
|
|
13
|
+
meta_title?: string;
|
|
14
|
+
meta_description?: string;
|
|
15
|
+
role?: 'Contributor' | 'Author' | 'Editor' | 'Administrator';
|
|
16
|
+
default?: string;
|
|
17
|
+
};
|
|
18
|
+
export type AuthorDataObject = {
|
|
19
|
+
data: AuthorObject;
|
|
20
|
+
};
|
|
21
|
+
export default class AuthorContext extends MigrateBase {
|
|
22
|
+
#private;
|
|
23
|
+
data: any;
|
|
24
|
+
constructor(args?: any);
|
|
25
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import MigrateBase from './MigrateBase.js';
|
|
2
|
+
export default class AuthorContext extends MigrateBase {
|
|
3
|
+
#schema;
|
|
4
|
+
data = {};
|
|
5
|
+
constructor(args) {
|
|
6
|
+
super();
|
|
7
|
+
let initialData = {};
|
|
8
|
+
// If object supplied does not contain `initialData` or `schema`, use as initialData
|
|
9
|
+
if (typeof args === 'object' && !args.initialData && !args.schema) {
|
|
10
|
+
initialData = args;
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
initialData = args?.initialData ?? {};
|
|
14
|
+
}
|
|
15
|
+
// Define what fields are allowed, their types, validations, and defaults
|
|
16
|
+
this.#schema = {
|
|
17
|
+
name: { require: true, type: 'string', maxLength: 191 },
|
|
18
|
+
slug: { require: true, type: 'string', maxLength: 191 },
|
|
19
|
+
email: { require: true, type: 'string', maxLength: 191 },
|
|
20
|
+
profile_image: { type: 'string', maxLength: 2000 },
|
|
21
|
+
cover_image: { type: 'string', maxLength: 2000 },
|
|
22
|
+
bio: { type: 'text', maxLength: 200 },
|
|
23
|
+
website: { type: 'string', maxLength: 2000 },
|
|
24
|
+
location: { type: 'text', maxLength: 150 },
|
|
25
|
+
facebook: { type: 'string', maxLength: 2000 },
|
|
26
|
+
twitter: { type: 'string', maxLength: 2000 },
|
|
27
|
+
meta_title: { type: 'string', maxLength: 300 },
|
|
28
|
+
meta_description: { type: 'string', maxLength: 500 },
|
|
29
|
+
role: { required: true, type: 'string', choices: ['Contributor', 'Author', 'Editor', 'Administrator'], default: 'Contributor' }
|
|
30
|
+
};
|
|
31
|
+
this.schema = this.#schema;
|
|
32
|
+
// Push entires from the schema into the working object
|
|
33
|
+
Object.entries(this.#schema).forEach((item) => {
|
|
34
|
+
const [key, value] = item;
|
|
35
|
+
this.data[key] = value.default ?? null;
|
|
36
|
+
});
|
|
37
|
+
// Set initial data if provided
|
|
38
|
+
Object.entries(initialData).forEach(([key, value]) => {
|
|
39
|
+
this.data[key] = value;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=AuthorContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuthorContext.js","sourceRoot":"","sources":["../../src/lib/AuthorContext.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,MAAM,kBAAkB,CAAC;AAuB3C,MAAM,CAAC,OAAO,OAAO,aAAc,SAAQ,WAAW;IAClD,OAAO,CAAC;IACR,IAAI,GAAQ,EAAE,CAAC;IAEf,YAAY,IAAU;QAClB,KAAK,EAAE,CAAC;QAER,IAAI,WAAW,GAAG,EAAE,CAAC;QAErB,oFAAoF;QACpF,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,IAAI,CAAC,WAAW,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;YAChE,WAAW,GAAG,IAAI,CAAC;QACvB,CAAC;aAAM,CAAC;YACJ,WAAW,GAAG,IAAI,EAAE,WAAW,IAAI,EAAE,CAAC;QAC1C,CAAC;QAED,yEAAyE;QACzE,IAAI,CAAC,OAAO,GAAG;YACX,IAAI,EAAE,EAAC,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,EAAC;YACrD,IAAI,EAAE,EAAC,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,EAAC;YACrD,KAAK,EAAE,EAAC,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,EAAC;YACtD,aAAa,EAAE,EAAC,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,IAAI,EAAC;YAChD,WAAW,EAAE,EAAC,IAAI,EAAE,QAAQ,E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G,CAAC,GAAG,KAAK,CAAC;QAC3B,CAAC,CAAC,CAAC;IACP,CAAC;CACJ"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export default class MigrateBase {
|
|
2
|
+
#private;
|
|
3
|
+
schema: any;
|
|
4
|
+
data: any;
|
|
5
|
+
constructor();
|
|
6
|
+
validate(key: any, value: any): void;
|
|
7
|
+
get(prop: string): any;
|
|
8
|
+
set(prop: string, value: any): this;
|
|
9
|
+
remove(prop: any): this;
|
|
10
|
+
get getFinal(): any;
|
|
11
|
+
checkRequired(working: any): boolean;
|
|
12
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import errors from '@tryghost/errors';
|
|
2
|
+
export default class MigrateBase {
|
|
3
|
+
#context;
|
|
4
|
+
schema;
|
|
5
|
+
data;
|
|
6
|
+
constructor() {
|
|
7
|
+
this.#context = this.constructor.name;
|
|
8
|
+
}
|
|
9
|
+
validate(key, value) {
|
|
10
|
+
if (!this.schema[key] || value === null) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const type = this.schema[key].type;
|
|
14
|
+
const maxLength = this.schema[key].maxLength ?? null;
|
|
15
|
+
const choices = this.schema[key].choices ?? null;
|
|
16
|
+
if (maxLength && value?.length > maxLength) {
|
|
17
|
+
throw new errors.InternalServerError({
|
|
18
|
+
message: `(${this.#context}) Value for "${key}" is too long. Currently ${value.length} characters, Max ${maxLength}.`,
|
|
19
|
+
context: value
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
else if (type === 'dateTime') {
|
|
23
|
+
if (!(value instanceof Date)) {
|
|
24
|
+
throw new errors.InternalServerError({
|
|
25
|
+
message: `(${this.#context}) Invalid date value for ${key}`,
|
|
26
|
+
context: value
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
else if (type === 'string' && choices && choices.length) {
|
|
31
|
+
if (!choices.includes(value)) {
|
|
32
|
+
throw new errors.InternalServerError({
|
|
33
|
+
message: `(${this.#context}) Invalid choice for ${key}`,
|
|
34
|
+
context: value
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else if (type === 'boolean' && typeof value !== 'boolean') {
|
|
39
|
+
throw new errors.InternalServerError({
|
|
40
|
+
message: `(${this.#context}) Invalid boolean value for ${key}`,
|
|
41
|
+
context: value
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
else if (type === 'array' && !Array.isArray(value)) {
|
|
45
|
+
throw new errors.InternalServerError({
|
|
46
|
+
message: `(${this.#context}) Invalid array value for ${key}`,
|
|
47
|
+
context: value
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
#setProp(prop, value) {
|
|
52
|
+
if (this.schema[prop]) {
|
|
53
|
+
// NOTE: This is buggy
|
|
54
|
+
// if (Array.isArray(value)) {
|
|
55
|
+
// value.forEach((vItem) => {
|
|
56
|
+
// this.validate(prop, vItem);
|
|
57
|
+
// });
|
|
58
|
+
// } else {
|
|
59
|
+
// this.validate(prop, value);
|
|
60
|
+
// }
|
|
61
|
+
this.validate(prop, value);
|
|
62
|
+
this.data[prop] = value;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
throw new errors.InternalServerError({
|
|
66
|
+
message: `(${this.#context}) Property "${prop}" is not allowed in ${this.#context}`
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
get(prop) {
|
|
71
|
+
if (prop in this.schema) {
|
|
72
|
+
let value = this.data[prop];
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
throw new errors.InternalServerError({
|
|
77
|
+
message: `(${this.#context}) Property "${prop}" does not exist in ${this.#context}`
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
set(prop, value) {
|
|
82
|
+
if (Array.isArray(prop)) {
|
|
83
|
+
prop.forEach((item) => {
|
|
84
|
+
this.#setProp(item, value);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
this.#setProp(prop, value);
|
|
89
|
+
}
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
remove(prop) {
|
|
93
|
+
this.set(prop, this.schema[prop].default ?? null);
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
get getFinal() {
|
|
97
|
+
this.checkRequired(this.data);
|
|
98
|
+
let clone = Object.assign({}, this);
|
|
99
|
+
delete clone.schema;
|
|
100
|
+
return clone;
|
|
101
|
+
}
|
|
102
|
+
checkRequired(working) {
|
|
103
|
+
// Check required fields
|
|
104
|
+
const required = Object.keys(this.schema).filter(key => this.schema[key].required);
|
|
105
|
+
required.forEach((key) => {
|
|
106
|
+
if (working[key] === null || working[key] === undefined) {
|
|
107
|
+
throw new errors.InternalServerError({ message: `(${this.#context}) Missing required field: ${key}` });
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
// Validate values
|
|
111
|
+
for (const [key, value] of Object.entries(working)) {
|
|
112
|
+
this.validate(key, value);
|
|
113
|
+
}
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=MigrateBase.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MigrateBase.js","sourceRoot":"","sources":["../../src/lib/MigrateBase.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,kBAAkB,CAAC;AAEtC,MAAM,CAAC,OAAO,OAAO,WAAW;IAC5B,QAAQ,CAAC;IACT,MAAM,CAAM;IACZ,IAAI,CAAM;IAEV;QACI,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;IAC1C,CAAC;IAED,QAAQ,CAAC,GAAQ,EAAE,KAAU;QACzB,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YACtC,OAAO;QACX,CAAC;QAED,MAA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,CAAC,mBAAmB,CAAC,EAAC,OAAO,EAAE,IAAI,IAAI,CAAC,QAAQ,6BAA6B,GAAG,EAAE,EAAC,CAAC,CAAC;YACzG,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,kBAAkB;QAClB,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;YACjD,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC9B,CAAC;QAED,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import MigrateBase from './MigrateBase.js';
|
|
2
|
+
import PostContext, { PostConstructorOptions } from './PostContext.js';
|
|
3
|
+
import TagContext from './TagContext.js';
|
|
4
|
+
import AuthorContext from './AuthorContext.js';
|
|
5
|
+
export type FindPostsOptions = {
|
|
6
|
+
slug?: string;
|
|
7
|
+
title?: string;
|
|
8
|
+
sourceAttr?: any;
|
|
9
|
+
tagSlug?: string;
|
|
10
|
+
tagName?: string;
|
|
11
|
+
authorSlug?: string;
|
|
12
|
+
authorName?: string;
|
|
13
|
+
authorEmail?: string;
|
|
14
|
+
};
|
|
15
|
+
export type FindTagsOptions = {
|
|
16
|
+
slug?: string;
|
|
17
|
+
name?: string;
|
|
18
|
+
};
|
|
19
|
+
export type FindAuthorsOptions = {
|
|
20
|
+
slug?: string;
|
|
21
|
+
name?: string;
|
|
22
|
+
email?: string;
|
|
23
|
+
};
|
|
24
|
+
export default class MigrateContext extends MigrateBase {
|
|
25
|
+
#private;
|
|
26
|
+
constructor();
|
|
27
|
+
forEachPost(callback: Function): Promise<void>;
|
|
28
|
+
forEachPostSync(callback: Function): void;
|
|
29
|
+
addPost(post?: PostContext | PostConstructorOptions): PostContext;
|
|
30
|
+
findPosts({ slug, title, sourceAttr, tagSlug, tagName, authorSlug, authorName, authorEmail }?: FindPostsOptions): PostContext[] | null;
|
|
31
|
+
findTags({ slug, name }?: FindTagsOptions): TagContext[] | null;
|
|
32
|
+
findAuthors({ slug, name, email }?: FindAuthorsOptions): AuthorContext[] | null;
|
|
33
|
+
get allPosts(): any;
|
|
34
|
+
get ghostJson(): any;
|
|
35
|
+
}
|