@miso.ai/server-wordpress 0.6.3-beta.0 → 0.6.3-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/cli/entities.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { Transform } from 'stream';
1
2
  import { stream, parseDuration } from '@miso.ai/server-commons';
2
3
  import { WordPressClient } from '../src/index.js';
3
4
  import { normalizeOptions, normalizeTransform, parseDate } from './utils.js';
@@ -34,6 +35,11 @@ export function buildForEntities(yargs) {
34
35
  alias: 'include',
35
36
  describe: 'Specify post ids'
36
37
  })
38
+ .option('fields', {
39
+ describe: 'Specify which record fields are retrieved',
40
+ type: 'array',
41
+ coerce: yargs.coerceToArray,
42
+ })
37
43
  .option('resolve', {
38
44
  alias: 'r',
39
45
  describe: 'Attach resolved entities (author, catagories) linked with the subjects',
@@ -60,9 +66,17 @@ function build(yargs) {
60
66
  });
61
67
  }
62
68
 
63
- async function run({ count, terms, update, name, ...options }) {
69
+ async function run({ subcmd, count, terms, update, name, ...options }) {
64
70
  options = normalizeOptions(options);
65
71
  const client = new WordPressClient(options);
72
+ switch (subcmd) {
73
+ case 'ids':
74
+ await runIds(client, name, { update, ...options });
75
+ return;
76
+ case 'count':
77
+ await runCount(client, name, options);
78
+ return;
79
+ }
66
80
  if (count) {
67
81
  await runCount(client, name, options);
68
82
  } else if (terms) {
@@ -95,46 +109,71 @@ export async function runGet(client, name, { transform, ...options }) {
95
109
  );
96
110
  }
97
111
 
98
- export async function runUpdate(client, name, update, {
112
+ export async function runIds(client, name, { update, transform, resolve, fields, ...options }) {
113
+ if (update) {
114
+ await stream.pipeline(
115
+ await buildUpdateStream(client, name, update, { ...options, fields: ['id', 'modified_gmt'] }),
116
+ new Transform({
117
+ objectMode: true,
118
+ transform({ id }, _, callback) {
119
+ callback(null, id);
120
+ },
121
+ }),
122
+ new stream.OutputStream(),
123
+ );
124
+ } else {
125
+ await stream.pipeline(
126
+ await client.entities(name).ids(options),
127
+ new stream.OutputStream(),
128
+ );
129
+ }
130
+ }
131
+
132
+ export async function runUpdate(client, name, update, options) {
133
+ await stream.pipeline(
134
+ await buildUpdateStream(client, name, update, options),
135
+ new stream.OutputStream(),
136
+ );
137
+ }
138
+
139
+ async function buildUpdateStream(client, name, update, {
99
140
  date, after, before, orderBy, order, // strip off date filters and order criteria
100
141
  transform,
101
142
  ...options
102
143
  }) {
144
+ // TODO: move the logic into client itself
103
145
  transform = await normalizeTransform(transform);
104
146
  const now = Date.now();
105
147
  update = parseDuration(update);
106
148
  const threshold = now - update;
107
149
  const entities = client.entities(name);
108
- await stream.pipelineToStdout(
109
- stream.concat(
110
- ...await Promise.all([
111
- // get recent published
112
- entities.stream({
113
- ...options,
114
- transform,
115
- after: threshold,
116
- }),
117
- // get recent modified, excluding ones already fetched
118
- entities.stream({
119
- ...options,
120
- transform,
121
- orderBy: 'modified',
122
- before: threshold,
123
- pageSize: 20,
124
- strategy: {
125
- highWatermark: 100,
126
- eagerLoad: true,
127
- terminate: entity => parseDate(entity.modified_gmt) < threshold,
128
- },
129
- })
130
- ])
131
- ),
132
- stream.stringify(),
150
+ return stream.concat(
151
+ ...await Promise.all([
152
+ // get recent published
153
+ entities.stream({
154
+ ...options,
155
+ transform,
156
+ after: threshold,
157
+ }),
158
+ // get recent modified, excluding ones already fetched
159
+ entities.stream({
160
+ ...options,
161
+ transform,
162
+ orderBy: 'modified',
163
+ before: threshold,
164
+ pageSize: 20,
165
+ strategy: {
166
+ highWatermark: 100,
167
+ eagerLoad: true,
168
+ terminate: entity => parseDate(entity.modified_gmt) < threshold,
169
+ },
170
+ })
171
+ ])
133
172
  );
134
173
  }
135
174
 
136
175
  export default {
137
- command: ['$0 <name>'],
176
+ command: ['$0 <name> [subcmd]'],
138
177
  desc: 'List entities from WordPress REST API',
139
178
  builder: build,
140
179
  handler: run,
package/cli/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import { yargs } from '@miso.ai/server-commons';
3
3
  import version from '../src/version.js';
4
- import profile from './profile.js';
4
+ import { profile, init } from './profile.js';
5
5
  import taxonomies from './taxonomies.js';
6
6
  import entities from './entities.js';
7
7
 
@@ -21,6 +21,7 @@ yargs.build(yargs => {
21
21
  default: false,
22
22
  })
23
23
  .hide('debug')
24
+ .command(init)
24
25
  .command(profile)
25
26
  .command(taxonomies)
26
27
  .command(entities)
package/cli/profile.js CHANGED
@@ -11,7 +11,7 @@ function build(yargs) {
11
11
  });
12
12
  }
13
13
 
14
- async function run({ generate, ...options }) {
14
+ async function runProfile({ generate, ...options }) {
15
15
  if (generate) {
16
16
  await runGenerate(options);
17
17
  } else {
@@ -38,9 +38,19 @@ async function runView(options) {
38
38
  console.log(JSON.stringify(client.profile, undefined, 2));
39
39
  }
40
40
 
41
- export default {
41
+ async function runInit(options) {
42
+ await runGenerate(options);
43
+ }
44
+
45
+ export const profile = {
42
46
  command: 'profile',
43
47
  desc: 'WordPress site profile management',
44
48
  builder: build,
45
- handler: run,
49
+ handler: runProfile,
50
+ };
51
+
52
+ export const init = {
53
+ command: 'init <site>',
54
+ desc: 'Initialize WordPress site profile',
55
+ handler: runInit,
46
56
  };
package/package.json CHANGED
@@ -17,9 +17,9 @@
17
17
  "simonpai <simon.pai@askmiso.com>"
18
18
  ],
19
19
  "dependencies": {
20
- "@miso.ai/server-commons": "0.6.3-beta.0",
20
+ "@miso.ai/server-commons": "0.6.3-beta.2",
21
21
  "axios": "^0.27.2",
22
22
  "axios-retry": "^3.3.1"
23
23
  },
24
- "version": "0.6.3-beta.0"
24
+ "version": "0.6.3-beta.2"
25
25
  }
package/src/axios.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import axios from 'axios';
2
2
  import axiosRetry from 'axios-retry';
3
3
 
4
+ // TODO: create an instance
5
+
4
6
  axiosRetry(axios, { retries: 5, retryDelay: count => count * 300 });
5
7
 
6
8
  export default axios;
@@ -1,3 +1,4 @@
1
+ import { Transform } from 'stream';
1
2
  import { asArray, stream } from '@miso.ai/server-commons';
2
3
  import EntityIndex from './entity-index.js';
3
4
  import EntityTransformStream from './transform.js';
@@ -14,6 +15,7 @@ export default class Entities {
14
15
  }
15
16
 
16
17
  async stream({ resolve = false, transform, ...options } = {}) {
18
+ // TODO: when after/before is used and fields are specified, we need to retain only fields that user wants
17
19
  if (!resolve && !transform) {
18
20
  return this._client._helpers.stream(this.name, options);
19
21
  }
@@ -53,6 +55,21 @@ export default class Entities {
53
55
  .pipe(transformStream);
54
56
  }
55
57
 
58
+ async ids(options = {}) {
59
+ const { before, after, u } = options;
60
+ const fields = ['id'];
61
+ if (before || after) {
62
+ fields.push('modified_gmt');
63
+ }
64
+ return (await this._client._helpers.stream(this.name, { ...options, fields }))
65
+ .pipe(new Transform({
66
+ objectMode: true,
67
+ transform({ id }, _, callback) {
68
+ callback(null, id);
69
+ },
70
+ }));
71
+ }
72
+
56
73
  async getAll(options) {
57
74
  return stream.collect(await this.stream(options));
58
75
  }
package/src/helpers.js CHANGED
@@ -135,6 +135,7 @@ class Url {
135
135
  // https://make.wordpress.org/core/2021/02/23/rest-api-changes-in-wordpress-5-7/
136
136
  async append(url, options = {}) {
137
137
  const { after, before, order, orderBy, page, pageSize, offset, include, exclude } = options;
138
+ let { fields } = options;
138
139
  const params = [];
139
140
 
140
141
  // TODO: support single id
@@ -153,6 +154,12 @@ class Url {
153
154
  has(offset) && params.push(`offset=${offset}`);
154
155
  has(include) && include.length && params.push(`include=${joinIds(include)}`);
155
156
  has(exclude) && exclude.length && params.push(`exclude=${joinIds(exclude)}`);
157
+ if (has(fields) && fields.length) {
158
+ if (has(before) && !fields.includes('modified_gmt')) {
159
+ fields = [...fields, 'modified_gmt'];
160
+ }
161
+ params.push(`_fields=${fields.join(',')}`);
162
+ }
156
163
 
157
164
  const head = params.length === 0 ? '' : url.indexOf('?') < 0 ? '?' : '&';
158
165
  return `${url}${head}${params.join('&')}`;
package/src/version.js CHANGED
@@ -1 +1 @@
1
- export default '0.6.3-beta.0';
1
+ export default '0.6.3-beta.2';