Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TYPING] Added types for session #3025

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Conversation

GermanBluefox
Copy link
Contributor

Better typing for session to understand how it works

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Overview

This pull request introduces improved TypeScript typings for the session management module to clarify the expected types and function signatures.

  • Introduces a new SessionData type and refines the Session type
  • Updates function signatures for the get and set methods with enhanced type annotations
  • Adjusts callback handling in the set method

Reviewed Changes

File Description
packages/common-db/src/lib/common/session.ts Added type definitions and updated function signatures to improve type safety for session handling

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

@GermanBluefox GermanBluefox added the type definition update ⌚ Feature requires an update of the type definitions label Feb 27, 2025

type Session = {
Store: any;
};

// 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
interface AdapterStoreOptions {
/** The ioBroker adapter */
adapter: any;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why Any and not Adapter type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
IDK how to solve it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The types are not build when DB is build so can not be used. So maybe move AdapterStore out of Common-DB package when it is more Adapter specific?

* @returns the constructor to create a new AdapterStore
*/
export function createAdapterStore(session: Session, defaultTtl = 3600): any {
const Store = session.Store;

class AdapterStore extends Store {
private readonly adapter: any;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why any here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

if (typeof ttl === 'object') {
fn = sess;
fn = sess as (err?: Error | null) => void;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if think better check if typef sess === 'function' before hardly defining that type here whoich might be wrong

sess && sess.cookie && sess.cookie.originalMaxAge
? Math.round(sess.cookie.originalMaxAge / 1000)
: defaultTtl;
ttl = sess?.cookie?.originalMaxAge ? Math.round(sess.cookie.originalMaxAge / 1000) : defaultTtl;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is ttl === 0 allowed? could be result of this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If ttl===0 it will be set to default

}
ttl = ttl || defaultTtl;
this.adapter.setSession(sid, ttl, sess, function () {
this.adapter.setSession(sid, ttl, sess, function (err?: Error | null): void {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe use lambda function here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Because we do not want to give the 'this' context in callback

@GermanBluefox GermanBluefox requested a review from Apollon77 March 8, 2025 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type definition update ⌚ Feature requires an update of the type definitions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants