Skip to content

Commit 18857e4

Browse files
committed
better types (again)
1 parent 33ae2e7 commit 18857e4

4 files changed

Lines changed: 100 additions & 36 deletions

File tree

components/pryv/src/Auth/AuthController.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ class AuthController {
159159

160160
/**
161161
* Start the authentication request and polling process
162-
* @returns {Promise<void>}
162+
* @returns {Promise<AuthRequestResponse>} Promise resolving to the auth request response
163+
* @see https://pryv.github.io/reference/#auth-request
163164
*/
164165
async startAuthRequest () {
165166
this.state = await postAccess.call(this);

components/pryv/src/Browser/CookieUtils.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ module.exports = {
1818
/**
1919
* Set a local cookie
2020
* @memberof pryv.Browser.CookieUtils
21+
* @template T
2122
* @param {string} cookieKey - The key for the cookie
22-
* @param {mixed} value - The Value
23-
* @param {number} expireInDays - Expiration date in days from now
23+
* @param {T} value - The value (will be JSON stringified)
24+
* @param {number} [expireInDays=365] - Expiration date in days from now
2425
*/
2526
function set (cookieKey, value, expireInDays) {
2627
if (!utils.isBrowser()) return;
@@ -41,7 +42,9 @@ function set (cookieKey, value, expireInDays) {
4142
/**
4243
* Return the value of a local cookie
4344
* @memberof pryv.Browser.CookieUtils
44-
* @param cookieKey - The key
45+
* @template T
46+
* @param {string} cookieKey - The key
47+
* @returns {T|undefined} The parsed cookie value or undefined if not found
4548
*/
4649
function get (cookieKey) {
4750
const name = encodeURIComponent(cookieKey);
@@ -54,7 +57,7 @@ function get (cookieKey) {
5457
/**
5558
* Delete a local cookie
5659
* @memberof pryv.Browser.CookieUtils
57-
* @param cookieKey - The key
60+
* @param {string} cookieKey - The key
5861
*/
5962
function del (cookieKey) {
6063
set(cookieKey, { deleted: true }, -1);

components/pryv/src/Connection.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,12 @@ class Connection {
301301
}
302302

303303
/**
304-
* ADD Data Points to HFEvent (flatJSON format)
305-
* https://api.pryv.com/reference/#add-hf-series-data-points
304+
* Add data points to an HF (High Frequency) series event (flatJSON format)
305+
* @param {string} eventId - The HF event ID
306+
* @param {string[]} fields - Array of field names for the series
307+
* @param {Array<Array<number|string>>} points - Array of data points, each point is an array of values
308+
* @returns {Promise<HFSeriesAddResult>} Promise resolving to status response
309+
* @see https://api.pryv.com/reference/#add-hf-series-data-points
306310
*/
307311
async addPointsToHFEvent (eventId, fields, points) {
308312
const res = await this.post('events/' + eventId + '/series', {

components/pryv/src/index.d.ts

Lines changed: 85 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
declare module 'pryv' {
22
type Timestamp = number;
33
type Identifier = string;
4+
5+
/**
6+
* Common metadata returned by all API responses
7+
* @see https://api.pryv.com/reference/#in-method-results
8+
*/
9+
export type ApiMeta = {
10+
/** API version in format {major}.{minor}.{revision} */
11+
apiVersion: string;
12+
/** Current server time as Unix timestamp in seconds */
13+
serverTime: Timestamp;
14+
/** Serial that changes when core or register is updated */
15+
serial: string;
16+
};
417
export type PermissionLevel = 'read' | 'contribute' | 'manage' | 'create-only';
518
export type KeyValue = { [key: string]: string | number };
619

@@ -104,7 +117,16 @@ declare module 'pryv' {
104117
export type HFSeries = {
105118
format: 'flatJSON';
106119
fields: string[];
107-
points: Array<number | string>;
120+
points: Array<Array<number | string>>;
121+
};
122+
123+
/**
124+
* Response from adding data points to an HF series event
125+
* @see https://api.pryv.com/reference/#add-hf-series-data-points
126+
*/
127+
export type HFSeriesAddResult = {
128+
status: 'ok';
129+
meta: ApiMeta;
108130
};
109131

110132
type Run = {
@@ -537,11 +559,7 @@ declare module 'pryv' {
537559
export type StreamedEventsResult = {
538560
eventsCount?: number;
539561
eventsDeletionsCount?: number;
540-
meta: {
541-
apiVersion: string;
542-
serverTime: number;
543-
serial: string;
544-
};
562+
meta: ApiMeta;
545563
};
546564

547565
export type EventFileCreationParams = Partial<
@@ -604,10 +622,10 @@ declare module 'pryv' {
604622
filename: string,
605623
): Promise<EventAPICallRes>;
606624
addPointsToHFEvent(
607-
id: Identifier,
625+
eventId: Identifier,
608626
fields: string[],
609-
values: Array<string | number>,
610-
): Promise<any>;
627+
points: Array<Array<number | string>>,
628+
): Promise<HFSeriesAddResult>;
611629
accessInfo(): Promise<AccessInfo>;
612630
revoke(throwOnFail?: boolean, usingConnection?: Connection): Promise<any>;
613631
readonly deltaTime: number;
@@ -726,19 +744,19 @@ declare module 'pryv' {
726744
export type StateChangeTypes = {
727745
ERROR: {
728746
message?: string;
729-
error?: any;
747+
error?: Error | unknown;
730748
};
731749
LOADING: {};
732750
INITIALIZED: {
733751
serviceInfo: ServiceInfo;
734752
};
735753
NEED_SIGNIN: {
736754
authUrl: string;
737-
clientData: any;
738-
code: number;
755+
clientData?: KeyValue;
756+
code?: number;
739757
key: string;
740758
lang: string;
741-
oauthState: any;
759+
oauthState?: string;
742760
poll: string;
743761
poll_rate_ms: number;
744762
requestedPermissions: Array<{
@@ -747,24 +765,48 @@ declare module 'pryv' {
747765
defaultName: string;
748766
}>;
749767
requestingAppId: string;
750-
returnUrl: any;
751-
serviceInfo: ServiceInfo;
768+
returnUrl?: string | null;
769+
serviceInfo?: ServiceInfo;
752770
};
753771
ACCEPTED: {
754-
serviceInfo: ServiceInfo;
772+
serviceInfo?: ServiceInfo;
755773
apiEndpoint: string;
756774
username: string;
757-
token: string;
775+
token?: string;
758776
};
759777
SIGNOUT: {};
760-
REFUSED: {};
778+
REFUSED: {
779+
reasonID?: string;
780+
message?: string;
781+
serviceInfo?: ServiceInfo;
782+
};
761783
};
762784

763785
export type StateChange<K extends States> = StateChangeTypes[K] & {
764786
id: K;
765787
status: K;
766788
};
767789

790+
/**
791+
* Response from auth-request POST endpoint
792+
* @see https://pryv.github.io/reference/#auth-request
793+
*/
794+
export type AuthRequestResponse = {
795+
status: 'NEED_SIGNIN';
796+
authUrl: string;
797+
/** @deprecated Use authUrl instead */
798+
url?: string;
799+
key: string;
800+
poll: string;
801+
poll_rate_ms: number;
802+
requestingAppId: string;
803+
requestedPermissions: AuthRequestedPermission[];
804+
lang?: string;
805+
returnURL?: string;
806+
clientData?: KeyValue;
807+
serviceInfo?: ServiceInfo;
808+
};
809+
768810
export type AuthSettings = {
769811
spanButtonID?: string;
770812
onStateChange?: (state: StateChange<States>) => void;
@@ -775,15 +817,23 @@ declare module 'pryv' {
775817
requestedPermissions: AuthRequestedPermission[];
776818
returnUrl?: string | boolean;
777819
referer?: string;
778-
clientData?: Object;
820+
clientData?: KeyValue;
821+
deviceName?: string;
822+
expireAfter?: number;
823+
serviceInfo?: Partial<ServiceInfo>;
779824
};
780825
};
781826

827+
export type LoginButtonConstructor = new (
828+
authSettings: AuthSettings,
829+
service: Service,
830+
) => CustomLoginButton;
831+
782832
export type SetupAuth = (
783833
settings: AuthSettings,
784834
serviceInfoUrl: string,
785835
serviceCustomizations?: serviceCustomizations,
786-
humanInteraction?: any,
836+
humanInteraction?: LoginButtonConstructor,
787837
) => Promise<Service>;
788838

789839
export type AuthStates = {
@@ -799,14 +849,20 @@ declare module 'pryv' {
799849
type AuthStatePayload = {
800850
status: AuthStates[keyof AuthStates];
801851
message?: string;
852+
error?: Error | unknown;
802853
};
803854

855+
export type StoredAuthorizationData = {
856+
apiEndpoint: string;
857+
username: string;
858+
} | null;
859+
804860
export type CustomLoginButton = {
805861
init?: () => Promise<Service>;
806-
getAuthorizationData(): any;
862+
getAuthorizationData(): StoredAuthorizationData;
807863
onStateChange(state: AuthStatePayload): Promise<void>;
808864
onClick(): void;
809-
saveAuthorizationData?: (authData: any) => void;
865+
saveAuthorizationData?: (authData: StoredAuthorizationData) => void;
810866
deleteAuthorizationData?: () => Promise<void>;
811867
finishAuthProcessAfterRedirection?: (authController: AuthController) => Promise<void>;
812868
};
@@ -827,8 +883,8 @@ declare module 'pryv' {
827883
init(): Promise<Service>;
828884
onClick(): void;
829885
onStateChange(state: AuthStatePayload): Promise<void>;
830-
getAuthorizationData(): any;
831-
saveAuthorizationData(authData: any): void;
886+
getAuthorizationData(): StoredAuthorizationData;
887+
saveAuthorizationData(authData: StoredAuthorizationData): void;
832888
deleteAuthorizationData(): Promise<void>;
833889
finishAuthProcessAfterRedirection(authController: AuthController): Promise<void>;
834890
}
@@ -856,7 +912,7 @@ declare module 'pryv' {
856912
windowLocationForTest?: string,
857913
navigatorForTests?: string,
858914
): string | boolean;
859-
startAuthRequest(): Promise<any>;
915+
startAuthRequest(): Promise<AuthRequestResponse>;
860916
set state(newState: AuthStatePayload);
861917
get state(): AuthStatePayload;
862918
}
@@ -872,8 +928,8 @@ declare module 'pryv' {
872928
export const Browser: {
873929
LoginButton: typeof LoginButton;
874930
CookieUtils: {
875-
set(cookieKey: string, value: any, expireInDays?: number): void;
876-
get(cookieKey: string): any;
931+
set<T = unknown>(cookieKey: string, value: T, expireInDays?: number): void;
932+
get<T = unknown>(cookieKey: string): T | undefined;
877933
del(cookieKey: string): void;
878934
};
879935
AuthStates: AuthStates;
@@ -912,8 +968,8 @@ declare module 'pryv' {
912968
Browser: {
913969
LoginButton: typeof LoginButton;
914970
CookieUtils: {
915-
set(cookieKey: string, value: any, expireInDays?: number): void;
916-
get(cookieKey: string): any;
971+
set<T = unknown>(cookieKey: string, value: T, expireInDays?: number): void;
972+
get<T = unknown>(cookieKey: string): T | undefined;
917973
del(cookieKey: string): void;
918974
};
919975
AuthStates: AuthStates;

0 commit comments

Comments
 (0)