-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAxiosWikibaseMessagesRepo.ts
87 lines (76 loc) · 2.77 KB
/
AxiosWikibaseMessagesRepo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import TechnicalProblem from '@/common/data-access/error/TechnicalProblem';
import MessageNotFound from '@/common/data-access/error/MessageNotFound';
import MessageTranslationCollection from '@/datamodel/MessageTranslationCollection';
import MessageCollection from '@/datamodel/MessageCollection';
import MessagesRepository from '@/common/data-access/MessagesRepository';
import { AxiosInstance, AxiosResponse, AxiosError } from 'axios';
import { MEDIAWIKI_API_SCRIPT } from '@/common/constants';
import { MessageKey } from '@/common/MessageKey';
import AxiosTechnicalProblem from '@/common/data-access/error/AxiosTechnicalProblem';
interface ResponseMessageSuccess {
name: MessageKey;
normalizedname: string;
'*': string;
}
interface ResponseMessageMissing {
name: MessageKey;
missing: string;
}
type AllMessagesResponseMessage = ResponseMessageSuccess | ResponseMessageMissing;
export default class AxiosWikibaseMessagesRepo implements MessagesRepository {
private axios: AxiosInstance;
private messageKeys: MessageKey[];
public constructor( axios: AxiosInstance, messageKeys: MessageKey[] ) {
this.axios = axios;
this.messageKeys = messageKeys;
}
public getMessagesInLanguage( inLanguage: string ): Promise<MessageTranslationCollection> {
return new Promise( ( resolve, reject ) => {
this.axios.get( MEDIAWIKI_API_SCRIPT, { params: {
action: 'query',
meta: 'allmessages',
ammessages: this.messageKeys.join( '|' ),
amlang: inLanguage,
} } )
.then( ( response: AxiosResponse ) => {
const data = response.data;
if ( typeof data !== 'object' || !( 'query' in data ) ) {
reject( new TechnicalProblem( 'allmessages result not well formed.' ) );
return;
}
if ( !( 'allmessages' in data.query ) ) {
reject( new TechnicalProblem( 'allmessages result not well formed.' ) );
return;
}
try {
resolve( this.getMessageTranslationCollection( inLanguage, data.query.allmessages ) );
} catch ( error ) {
reject( error );
return;
}
} )
.catch( ( error: AxiosError ) => {
reject( new AxiosTechnicalProblem( error ) );
} );
} );
}
private getMessageTranslationCollection(
inLanguage: string,
response: AllMessagesResponseMessage[],
): MessageTranslationCollection {
return {
[ inLanguage ]: this.transformMessages( response ),
};
}
private transformMessages( response: AllMessagesResponseMessage[] ): MessageCollection {
const messageList: MessageCollection = {};
response.forEach( ( value: ResponseMessageSuccess | ResponseMessageMissing ) => {
if ( 'missing' in value ) {
throw new MessageNotFound( `${value.name} is not a valid message-key.` );
} else {
messageList[ value.name ] = ( value as ResponseMessageSuccess )[ '*' ];
}
} );
return messageList;
}
}