feat(tracker): method to get session url

This commit is contained in:
sylenien 2022-08-30 12:27:33 +02:00
parent a4cdf69ae4
commit 7f3f013957
4 changed files with 38 additions and 4 deletions

View file

@ -27,7 +27,7 @@ module.exports = {
'@typescript-eslint/camelcase': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/unbound-method': 'off',
'@typescript-eslint/explicit-function-return-type': 'warn',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/prefer-readonly': 'warn',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
@ -46,4 +46,4 @@ module.exports = {
'@typescript-eslint/no-unused-expressions': 'warn',
'@typescript-eslint/no-useless-constructor': 'warn',
},
};
};

View file

@ -290,6 +290,11 @@ export default class App {
getSessionID(): string | undefined {
return this.session.getInfo().sessionID || undefined
}
getSessionURL(): string {
return this.session.getSessionURL()
}
getHost(): string {
return new URL(this.options.ingestPoint).hostname
}
@ -409,6 +414,7 @@ export default class App {
token,
userUUID,
sessionID,
projectID,
beaconSizeLimit,
startTimestamp, // real startTS, derived from sessionID
} = r
@ -423,7 +429,13 @@ export default class App {
}
this.session.setSessionToken(token)
this.localStorage.setItem(this.options.local_uuid_key, userUUID)
this.session.update({ sessionID, timestamp: startTimestamp || timestamp }) // TODO: no no-explicit 'any'
this.session.update({ sessionID, timestamp: startTimestamp || timestamp, projectID }) // TODO: no no-explicit 'any'
this.session.buildURL(
`${this.options.ingestPoint.replace(/\/ingest$/, '')}/${projectID as string}/session/${
sessionID as string
}`,
)
const startWorkerMsg: WorkerMessageData = {
type: 'auth',
token,

View file

@ -5,6 +5,7 @@ interface SessionInfo {
metadata: Record<string, string>
userID: string | null
timestamp: number
projectID?: string
}
type OnUpdateCallback = (i: Partial<SessionInfo>) => void
@ -19,6 +20,8 @@ export default class Session {
private sessionID: string | undefined
private readonly callbacks: OnUpdateCallback[] = []
private timestamp = 0
private projectID: string | undefined
private sessionUrl: string
constructor(private readonly app: App, private options: Options) {}
@ -35,7 +38,7 @@ export default class Session {
this.callbacks.forEach((cb) => cb(newInfo))
}
update(newInfo: Partial<SessionInfo>) {
update(newInfo: Partial<SessionInfo>): void {
if (newInfo.userID !== undefined) {
// TODO clear nullable/undefinable types
this.userID = newInfo.userID
@ -49,6 +52,9 @@ export default class Session {
if (newInfo.timestamp !== undefined) {
this.timestamp = newInfo.timestamp
}
if (newInfo.projectID !== undefined) {
this.projectID = newInfo.projectID
}
this.handleUpdate(newInfo)
}
@ -116,9 +122,18 @@ export default class Session {
metadata: this.metadata,
userID: this.userID,
timestamp: this.timestamp,
projectID: this.projectID,
}
}
buildURL(url: string): void {
this.sessionUrl = url
}
getSessionURL(): string {
return this.sessionUrl
}
reset(): void {
this.app.sessionStorage.removeItem(this.options.session_token_key)
this.metadata = {}

View file

@ -209,6 +209,13 @@ export default class API {
return this.getSessionID()
}
getSessionURL(): string | null {
if (this.app === null) {
return null
}
return this.app.getSessionURL()
}
setUserID(id: string): void {
if (typeof id === 'string' && this.app !== null) {
this.app.session.setUserID(id)