Skip to content

Commit f7c6185

Browse files
committed
fix: Incorrect response parser of wordpress.com.
1 parent 15e2d15 commit f7c6185

2 files changed

Lines changed: 38 additions & 8 deletions

File tree

src/wp-rest-client.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { AbstractWordPressClient } from './abstract-wp-client';
99
import WordpressPlugin from './main';
1010
import { Term } from './wp-api';
1111
import { RestClient } from './rest-client';
12-
import { isFunction, isString, template } from 'lodash-es';
12+
import { isArray, isFunction, isString, template } from 'lodash-es';
1313
import { SafeAny } from './utils';
1414
import { WpProfile } from './wp-profile';
1515

@@ -103,7 +103,22 @@ export class WpRestClient extends AbstractWordPressClient {
103103
{
104104
headers: this.context.getHeaders(certificate)
105105
})
106-
.then(data => data as Term[] ?? []);
106+
.then(data => {
107+
if (isArray(data)) {
108+
return data as Term[] ?? [];
109+
} else {
110+
if ((data as SafeAny).hasOwnProperty('found')) {
111+
// returns by wordpress.com
112+
return (data as SafeAny)
113+
.categories
114+
.map((it: Term & { ID: number }) => ({
115+
...it,
116+
id: String(it.ID)
117+
}));
118+
}
119+
}
120+
return [];
121+
});
107122
}
108123

109124
validateUser(certificate: WordPressAuthParams): Promise<WordPressClientResult> {
@@ -134,9 +149,22 @@ export class WpRestClient extends AbstractWordPressClient {
134149
name
135150
}),
136151
)
137-
.then((resp: SafeAny) => {
138-
console.log('WpRestClient getTags response', resp);
139-
return resp as Term[] ?? [];
152+
.then((data: SafeAny) => {
153+
console.log('WpRestClient getTags response', data);
154+
if (isArray(data)) {
155+
return data as Term[] ?? [];
156+
} else {
157+
if ((data as SafeAny).hasOwnProperty('found')) {
158+
// returns by wordpress.com
159+
return (data as SafeAny)
160+
.tags
161+
.map((it: Term & { ID: number }) => ({
162+
...it,
163+
id: String(it.ID)
164+
}));
165+
}
166+
}
167+
return [];
140168
});
141169
if (exists.length === 0) {
142170
return await this.client.httpPost(
@@ -149,7 +177,10 @@ export class WpRestClient extends AbstractWordPressClient {
149177
})
150178
.then((resp: SafeAny) => {
151179
console.log('WpRestClient newTag response', resp);
152-
return resp;
180+
return {
181+
...resp,
182+
id: resp.id ?? resp.ID
183+
};
153184
});
154185
} else {
155186
return exists[0];
@@ -198,7 +229,6 @@ interface WpRestClientContext {
198229

199230
getHeaders(wp: WordPressAuthParams): Record<string, string>;
200231

201-
202232
}
203233

204234
export class WpRestClientMiniOrangeContext implements WpRestClientContext {

src/wp-xml-rpc-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export class WpXmlRpcClient extends AbstractWordPressClient {
140140
});
141141
}
142142

143-
async getTag(name: string, certificate: WordPressAuthParams): Promise<Term> {
143+
getTag(name: string, certificate: WordPressAuthParams): Promise<Term> {
144144
return Promise.resolve({
145145
id: name,
146146
name,

0 commit comments

Comments
 (0)