11import { Keystore } from "./keystore" ;
2- import fetch from ' node-fetch' ;
3- import { getSignatureValues } from ' @wulkanowy/uonet-request-signer-node-hebe' ;
2+ import fetch from " node-fetch" ;
3+ import { getSignatureValues } from " @wulkanowy/uonet-request-signer-node-hebe" ;
44import { ApiHelper } from "./apiHelper" ;
5- import { APP_NAME , APP_OS , APP_USER_AGENT , APP_VERSION , millis , nowIso , uuid } from "./utils" ;
5+ import {
6+ APP_NAME ,
7+ APP_OS ,
8+ APP_USER_AGENT ,
9+ APP_VERSION ,
10+ millis ,
11+ nowIso ,
12+ uuid ,
13+ } from "./utils" ;
614import { Student } from "./interfaces/student" ;
715import { Account } from "./interfaces/account" ;
816
917export class Api {
10- private keystore : Keystore ;
11- public account ?: any ;
12- private restUrl ?: any ;
13- public student ?: any ;
14- public period ?: any ;
15- public helper : ApiHelper ;
18+ private keystore : Keystore ;
19+ public account ?: any ;
20+ private restUrl ?: any ;
21+ public student ?: any ;
22+ public period ?: any ;
23+ public helper : ApiHelper ;
1624
17- constructor ( keystore : Keystore , account ?: Account ) {
18- this . keystore = keystore ;
19- if ( account ) {
20- this . account = account ;
21- this . restUrl = account . restUrl ;
22- }
23- this . helper = new ApiHelper ( this ) ;
25+ constructor ( keystore : Keystore , account ?: Account ) {
26+ this . keystore = keystore ;
27+ if ( account ) {
28+ this . account = account ;
29+ this . restUrl = account . restUrl ;
2430 }
31+ this . helper = new ApiHelper ( this ) ;
32+ }
2533
26- private buildPayload = ( envelope : any ) => ( {
27- " AppName" : APP_NAME ,
28- " AppVersion" : APP_VERSION ,
29- " CertificateId" : this . keystore . fingerprint ,
30- " Envelope" : envelope ,
31- " FirebaseToken" : this . keystore . firebaseToken ,
32- " API" : 1 ,
33- " RequestId" : uuid ( ) ,
34- " Timestamp" : millis ( ) ,
35- " TimestampFormatted" : nowIso ( ) ,
36- } ) ;
34+ private buildPayload = ( envelope : any ) => ( {
35+ AppName : APP_NAME ,
36+ AppVersion : APP_VERSION ,
37+ CertificateId : this . keystore . fingerprint ,
38+ Envelope : envelope ,
39+ FirebaseToken : this . keystore . firebaseToken ,
40+ API : 1 ,
41+ RequestId : uuid ( ) ,
42+ Timestamp : millis ( ) ,
43+ TimestampFormatted : nowIso ( ) ,
44+ } ) ;
3745
38- private buildHeaders = ( fullUrl : string , payload : string ) => {
39- if ( ! this . keystore . fingerprint || ! this . keystore . privateKey ) {
40- throw Error ( "Keystore is not initialized!" ) ;
41- }
42- const dt = new Date ( ) ;
43- const { digest, canonicalUrl, signature} = getSignatureValues (
44- this . keystore . fingerprint ,
45- this . keystore . privateKey ,
46- payload ,
47- fullUrl ,
48- dt . toUTCString ( )
49- ) ;
50-
51- const headers : any = {
52- "User-Agent" : APP_USER_AGENT ,
53- "vOS" : APP_OS ,
54- "vDeviceModel" : this . keystore . deviceModel ,
55- "vAPI" : "1" ,
56- "vDate" : dt . toUTCString ( ) ,
57- "vCanonicalUrl" : canonicalUrl ,
58- "Signature" : signature ,
59- }
46+ private buildHeaders = ( fullUrl : string , payload : string ) => {
47+ if ( ! this . keystore . fingerprint || ! this . keystore . privateKey ) {
48+ throw Error ( "Keystore is not initialized!" ) ;
49+ }
50+ const dt = new Date ( ) ;
51+ const { digest, canonicalUrl, signature } = getSignatureValues (
52+ this . keystore . fingerprint ,
53+ this . keystore . privateKey ,
54+ payload ,
55+ fullUrl ,
56+ dt . toUTCString ( )
57+ ) ;
6058
61- if ( digest ) {
62- headers [ "Digest" ] = digest ;
63- headers [ "Content-Type" ] = "application/json" ;
64- }
59+ const headers : any = {
60+ "User-Agent" : APP_USER_AGENT ,
61+ vOS : APP_OS ,
62+ vDeviceModel : this . keystore . deviceModel ,
63+ vAPI : "1" ,
64+ vDate : dt . toUTCString ( ) ,
65+ vCanonicalUrl : canonicalUrl ,
66+ Signature : signature ,
67+ } ;
6568
66- return headers ;
69+ if ( digest ) {
70+ headers [ "Digest" ] = digest ;
71+ headers [ "Content-Type" ] = "application/json" ;
6772 }
6873
69- private request = async ( method : "GET" | "POST" , url : string , body ?: any ) => {
70- const fullUrl = url . startsWith ( "http" ) ? url : this . restUrl ? this . restUrl + url : undefined ;
71- if ( ! fullUrl ) { throw Error ( "Relative URL specified but no account loaded!" ) }
72- const payload = body && method === "POST" ? JSON . stringify ( this . buildPayload ( body ) ) : null ;
73- const headers = this . buildHeaders ( fullUrl , payload === null ? "" : payload ) ;
74- const options : any = {
75- headers : headers ,
76- method : method
77- }
78- if ( payload !== null ) {
79- options [ "body" ] = payload ;
80- }
81- try {
82- const rawRes = await fetch ( fullUrl , options ) ;
83- const jsonRes = await rawRes . json ( ) ;
84- const status = jsonRes [ 'Status' ] ;
85- const envelope = jsonRes [ 'Envelope' ] ;
86- if ( status . Code !== 0 ) {
87- throw Error ( status [ "Message" ] ) ;
88- }
89- return envelope ;
90- } catch ( e ) {
91- throw Error ( e ) ;
92- }
93- }
74+ return headers ;
75+ } ;
9476
95- public get = async ( url : string , inQuery ?: any ) => {
96- const query = inQuery ? `${ ( ( ) => {
97- let queryToReturn = "" ;
98- for ( let item in inQuery ) {
99- queryToReturn += `&${ item } =${ encodeURIComponent ( inQuery [ item ] ) } ` ;
100- }
101- return queryToReturn . substr ( 1 ) ;
102- } ) ( ) } ` : undefined ;
77+ private request = async ( method : "GET" | "POST" , url : string , body ?: any ) => {
78+ const fullUrl = url . startsWith ( "http" )
79+ ? url
80+ : this . restUrl
81+ ? this . restUrl + url
82+ : undefined ;
83+ if ( ! fullUrl ) {
84+ throw Error ( "Relative URL specified but no account loaded!" ) ;
85+ }
86+ const payload =
87+ body && method === "POST"
88+ ? JSON . stringify ( this . buildPayload ( body ) )
89+ : null ;
90+ const headers = this . buildHeaders ( fullUrl , payload === null ? "" : payload ) ;
91+ const options : any = {
92+ headers : headers ,
93+ method : method ,
94+ } ;
95+ if ( payload !== null ) {
96+ options [ "body" ] = payload ;
97+ }
98+ try {
99+ const rawRes = await fetch ( fullUrl , options ) ;
100+ const jsonRes = await rawRes . json ( ) ;
101+ const status = jsonRes [ "Status" ] ;
102+ const envelope = jsonRes [ "Envelope" ] ;
103+ if ( status . Code !== 0 ) {
104+ throw Error ( status [ "Message" ] ) ;
105+ }
106+ return envelope ;
107+ } catch ( e ) {
108+ throw e ;
109+ }
110+ } ;
103111
104- if ( query ) {
105- url += `?${ query } ` ;
106- }
112+ public get = async ( url : string , inQuery ?: any ) => {
113+ const query = inQuery
114+ ? `${ ( ( ) => {
115+ let queryToReturn = "" ;
116+ for ( let item in inQuery ) {
117+ queryToReturn += `&${ item } =${ encodeURIComponent ( inQuery [ item ] ) } ` ;
118+ }
119+ return queryToReturn . substr ( 1 ) ;
120+ } ) ( ) } `
121+ : undefined ;
107122
108- return await this . request ( "GET" , url ) ;
123+ if ( query ) {
124+ url += `?${ query } ` ;
109125 }
110126
111- public post = async ( url : string , body : any ) => await this . request ( "POST" , url , body ) ;
127+ return await this . request ( "GET" , url ) ;
128+ } ;
112129
113- public setStudent ( student : Student ) {
114- this . student = student ;
115- this . restUrl = `${ this . restUrl } ${ student . unit . symbol } /` ;
116- this . period = student . periods . filter ( item => item . current ) [ 0 ] ;
117- }
118- }
130+ public post = async ( url : string , body : any ) =>
131+ await this . request ( "POST" , url , body ) ;
132+
133+ public setStudent ( student : Student ) {
134+ this . student = student ;
135+ this . restUrl = `${ this . restUrl } ${ student . unit . symbol } /` ;
136+ this . period = student . periods . filter ( ( item ) => item . current ) [ 0 ] ;
137+ }
138+ }
0 commit comments