Skip to content

Commit ebf87a3

Browse files
[TYPING] Added types for session (#3025)
* Added types for session * Update packages/common-db/src/lib/common/session.ts Co-authored-by: Copilot <[email protected]> * Fixing typing * Update build ubuntu to 22.04 (from 20.04) --------- Co-authored-by: Copilot <[email protected]>
1 parent 0f4381e commit ebf87a3

File tree

3 files changed

+52
-24
lines changed

3 files changed

+52
-24
lines changed

packages/common-db/src/lib/common/session.ts

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
1-
type Session = any;
1+
type SessionData = Record<string, any>;
2+
3+
type Session = {
4+
Store: any;
5+
};
26

37
// TODO: in the long term move this file somewhere where we have types access it is nowhere used in controller itself and just exported for adapters so it should go to js-controller-adapter package
48
interface AdapterStoreOptions {
59
/** The ioBroker adapter */
610
adapter: any;
711
/** The cookie */
8-
cookie: any;
12+
cookie?: {
13+
maxAge?: number;
14+
originalMaxAge?: number;
15+
};
916
}
1017

1118
/**
1219
* Function to create an AdapterStore constructor
1320
*
14-
* @param session The session object
15-
* @param defaultTtl the default time to live
21+
* @param session The session object, like "express-session"
22+
* @param defaultTtl the default time to live in seconds
1623
* @returns the constructor to create a new AdapterStore
1724
*/
1825
export function createAdapterStore(session: Session, defaultTtl = 3600): any {
1926
const Store = session.Store;
2027

2128
class AdapterStore extends Store {
29+
private readonly adapter: any;
30+
2231
constructor(options: AdapterStoreOptions) {
2332
super(options);
2433

@@ -37,20 +46,36 @@ export function createAdapterStore(session: Session, defaultTtl = 3600): any {
3746
* @param sid Session ID
3847
* @param fn callback
3948
*/
40-
get(sid: string, fn: (err?: any, obj?: any) => void): void {
41-
this.adapter.getSession(sid, (obj: any) => {
49+
get(sid: string, fn: (err?: Error | string | null, obj?: SessionData) => void): void {
50+
this.adapter.getSession(sid, (obj: SessionData): void => {
4251
if (obj) {
4352
if (fn) {
4453
return fn(null, obj);
4554
}
46-
} else {
47-
if (fn) {
48-
return fn();
49-
}
55+
} else if (fn) {
56+
return fn();
5057
}
5158
});
5259
}
5360

61+
/**
62+
* Commit the given `sess` object associated with the given `sid`.
63+
*
64+
* @param sid Session ID
65+
* @param sess the session
66+
* @param fn callback
67+
*/
68+
set(sid: string, sess: SessionData, fn: (err?: Error | null) => void): void;
69+
/**
70+
* Commit the given `sess` object associated with the given `sid`.
71+
*
72+
* @param sid Session ID
73+
* @param ttl Time to live
74+
* @param sess the session
75+
* @param fn callback
76+
*/
77+
set(sid: string, ttl: number, sess: SessionData, fn: (err?: Error | null) => void): void;
78+
5479
/**
5580
* Commit the given `sess` object associated with the given `sid`.
5681
*
@@ -59,22 +84,25 @@ export function createAdapterStore(session: Session, defaultTtl = 3600): any {
5984
* @param sess the session
6085
* @param fn callback
6186
*/
62-
set(sid: string, ttl: number, sess: Session, fn: (args: any) => void): void {
63-
if (typeof ttl === 'object') {
64-
fn = sess;
87+
set(sid: unknown, ttl: unknown, sess: unknown, fn?: unknown): void {
88+
if (typeof sess === 'function') {
89+
fn = sess as (err?: Error | null) => void;
6590
sess = ttl;
6691
// analyse if the session is stored directly from express session
67-
ttl =
68-
sess && sess.cookie && sess.cookie.originalMaxAge
69-
? Math.round(sess.cookie.originalMaxAge / 1000)
70-
: defaultTtl;
92+
ttl = (sess as SessionData)?.cookie?.originalMaxAge
93+
? Math.round((sess as SessionData).cookie.originalMaxAge / 1000)
94+
: defaultTtl;
7195
}
7296
ttl = ttl || defaultTtl;
73-
this.adapter.setSession(sid, ttl, sess, function () {
74-
// @ts-expect-error fix later
75-
// eslint-disable-next-line prefer-rest-params
76-
fn && fn.apply(this, arguments);
77-
}); // do not use here => !!!
97+
this.adapter.setSession(
98+
sid as string,
99+
ttl as number,
100+
sess as SessionData,
101+
function (err?: Error | null): void {
102+
// @ts-expect-error fix later
103+
fn?.call(this, err);
104+
},
105+
); // do not use here => !!!
78106
}
79107

80108
/**

packages/types-dev/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ declare global {
138138
interface Permission {
139139
/** The type of the permission */
140140
type: string;
141-
/** Which kind of operation is required */
141+
/** Defines which kind of operation is required */
142142
operation: string;
143143
}
144144
interface ObjectOrStatePermission extends Permission {

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"declaration": true, // generate .d.ts files
1111
"declarationMap": true, // generate .map files for .d.ts files
1212
"sourceMap": true, // generate .map files for .js files
13-
"inlineSourceMap": false, // Alternatively inline the source maps
13+
"inlineSourceMap": false, // Alternatively, inline the source maps
1414
// Don't emit anything by default. This will be turned on in the subprojects
1515
"noEmit": true,
1616
// Avoid TS errors in external dependencies

0 commit comments

Comments
 (0)