@empiricalrun/playwright-utils 0.16.2 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/auth/index.d.ts +9 -0
- package/dist/auth/index.d.ts.map +1 -1
- package/dist/auth/index.js +36 -1
- package/dist/auth/types.d.ts +24 -0
- package/dist/auth/types.d.ts.map +1 -0
- package/dist/auth/types.js +2 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @empiricalrun/playwright-utils
|
|
2
2
|
|
|
3
|
+
## 0.17.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 9f0a413: feat: auth store can extract cookies/localstorage
|
|
8
|
+
|
|
9
|
+
## 0.16.3
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 67f6e79: feat: return logged in page from AuthStore
|
|
14
|
+
|
|
3
15
|
## 0.16.2
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/dist/auth/index.d.ts
CHANGED
|
@@ -3,6 +3,11 @@ type WithKey<T> = T & {
|
|
|
3
3
|
key: string;
|
|
4
4
|
};
|
|
5
5
|
type AppType = "web-app" | "chrome-extension";
|
|
6
|
+
type QueryFilter = {
|
|
7
|
+
type: "cookie" | "localStorage";
|
|
8
|
+
origin: string;
|
|
9
|
+
name: string;
|
|
10
|
+
};
|
|
6
11
|
export declare class AuthStore<T extends WithKey<{}>> {
|
|
7
12
|
appType: AppType;
|
|
8
13
|
page: Page;
|
|
@@ -17,6 +22,10 @@ export declare class AuthStore<T extends WithKey<{}>> {
|
|
|
17
22
|
appType?: AppType;
|
|
18
23
|
});
|
|
19
24
|
loginIfRequired(): Promise<void>;
|
|
25
|
+
loggedInPage(): Promise<Page | undefined>;
|
|
26
|
+
static getValueFromAuthState(userContext: {
|
|
27
|
+
key: string;
|
|
28
|
+
}, query: QueryFilter): string | undefined;
|
|
20
29
|
private newPageWithAuthState;
|
|
21
30
|
private saveAuthState;
|
|
22
31
|
static location(userContext: {
|
package/dist/auth/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAU,IAAI,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAU,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAI/D,KAAK,OAAO,CAAC,CAAC,IAAI,CAAC,GAAG;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtC,KAAK,OAAO,GAAG,SAAS,GAAG,kBAAkB,CAAC;AAE9C,KAAK,WAAW,GAAG;IACjB,IAAI,EAAE,QAAQ,GAAG,cAAc,CAAC;IAChC,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,qBAAa,SAAS,CAAC,CAAC,SAAS,OAAO,CAAC,EAAE,CAAC;IAC1C,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,IAAI,CAAC;IACX,WAAW,EAAE,CAAC,CAAC;IACf,SAAS,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACzD,iBAAiB,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;gBACxD,EACV,IAAI,EACJ,WAAW,EACX,SAAS,EACT,iBAAiB,EACjB,OAAmB,GACpB,EAAE;QACD,IAAI,EAAE,IAAI,CAAC;QACX,WAAW,EAAE,CAAC,CAAC;QACf,SAAS,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;QACzD,iBAAiB,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;QACpE,OAAO,CAAC,EAAE,OAAO,CAAC;KACnB;IAQK,eAAe;IAIf,YAAY,IAAI,OAAO,CAAC,IAAI,GAAG,SAAS,CAAC;IAqC/C,MAAM,CAAC,qBAAqB,CAC1B,WAAW,EAAE;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,EAC5B,KAAK,EAAE,WAAW,GACjB,MAAM,GAAG,SAAS;YA0BP,oBAAoB;YAsBpB,aAAa;IA6B3B,MAAM,CAAC,QAAQ,CAAC,WAAW,EAAE;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM;WAIxC,eAAe,CAC1B,OAAO,EAAE,cAAc,EACvB,WAAW,EAAE,OAAO,CAAC,GAAG,CAAC;CAoB5B"}
|
package/dist/auth/index.js
CHANGED
|
@@ -21,7 +21,11 @@ class AuthStore {
|
|
|
21
21
|
this.checkLoggedInFunc = checkLoggedInFunc;
|
|
22
22
|
}
|
|
23
23
|
async loginIfRequired() {
|
|
24
|
+
await this.loggedInPage();
|
|
25
|
+
}
|
|
26
|
+
async loggedInPage() {
|
|
24
27
|
let shouldDoLogin = false;
|
|
28
|
+
let loggedInPage = undefined;
|
|
25
29
|
const storagePath = AuthStore.location(this.userContext);
|
|
26
30
|
if (!fs_1.default.existsSync(storagePath)) {
|
|
27
31
|
shouldDoLogin = true;
|
|
@@ -30,8 +34,13 @@ class AuthStore {
|
|
|
30
34
|
const page = await this.newPageWithAuthState(this.page, storagePath);
|
|
31
35
|
try {
|
|
32
36
|
const hasLoggedIn = await this.checkLoggedInFunc(page, this.userContext);
|
|
37
|
+
if (!hasLoggedIn) {
|
|
38
|
+
await page.close();
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
loggedInPage = page;
|
|
42
|
+
}
|
|
33
43
|
shouldDoLogin = !hasLoggedIn;
|
|
34
|
-
await page.close();
|
|
35
44
|
}
|
|
36
45
|
catch (err) {
|
|
37
46
|
if (err instanceof playwright_core_1.errors.TimeoutError) {
|
|
@@ -45,6 +54,32 @@ class AuthStore {
|
|
|
45
54
|
if (shouldDoLogin) {
|
|
46
55
|
await this.loginFunc(this.page, this.userContext);
|
|
47
56
|
await this.saveAuthState(storagePath);
|
|
57
|
+
loggedInPage = this.page;
|
|
58
|
+
}
|
|
59
|
+
return loggedInPage;
|
|
60
|
+
}
|
|
61
|
+
static getValueFromAuthState(userContext, query) {
|
|
62
|
+
console.log(query);
|
|
63
|
+
const storagePath = AuthStore.location(userContext);
|
|
64
|
+
const contents = fs_1.default.readFileSync(storagePath);
|
|
65
|
+
if (!contents) {
|
|
66
|
+
throw new Error("No auth state found");
|
|
67
|
+
}
|
|
68
|
+
const authState = JSON.parse(contents.toString());
|
|
69
|
+
if (query.type === "cookie") {
|
|
70
|
+
const cookie = authState.cookies.find((c) => c.name === query.name && c.domain === query.origin);
|
|
71
|
+
if (cookie) {
|
|
72
|
+
return cookie.value;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
else if (query.type === "localStorage") {
|
|
76
|
+
const origin = authState.origins.find((o) => o.origin === query.origin);
|
|
77
|
+
if (origin) {
|
|
78
|
+
const item = origin.localStorage.find((i) => i.name === query.name);
|
|
79
|
+
if (item) {
|
|
80
|
+
return item.value;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
48
83
|
}
|
|
49
84
|
}
|
|
50
85
|
async newPageWithAuthState(page, storagePath) {
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
type AuthStateCookie = {
|
|
2
|
+
name: string;
|
|
3
|
+
value: string;
|
|
4
|
+
domain: string;
|
|
5
|
+
path: string;
|
|
6
|
+
expires: number;
|
|
7
|
+
httpOnly: boolean;
|
|
8
|
+
secure: boolean;
|
|
9
|
+
sameSite: "Strict" | "Lax" | "None";
|
|
10
|
+
};
|
|
11
|
+
type AuthStateLocalStorage = {
|
|
12
|
+
name: string;
|
|
13
|
+
value: string;
|
|
14
|
+
};
|
|
15
|
+
type AuthStateOrigin = {
|
|
16
|
+
origin: string;
|
|
17
|
+
localStorage: AuthStateLocalStorage[];
|
|
18
|
+
};
|
|
19
|
+
export type AuthStateFileContent = {
|
|
20
|
+
cookies: AuthStateCookie[];
|
|
21
|
+
origins: AuthStateOrigin[];
|
|
22
|
+
};
|
|
23
|
+
export {};
|
|
24
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/auth/types.ts"],"names":[],"mappings":"AAAA,KAAK,eAAe,GAAG;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,OAAO,CAAC;IAClB,MAAM,EAAE,OAAO,CAAC;IAChB,QAAQ,EAAE,QAAQ,GAAG,KAAK,GAAG,MAAM,CAAC;CACrC,CAAC;AAEF,KAAK,qBAAqB,GAAG;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,KAAK,eAAe,GAAG;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,EAAE,qBAAqB,EAAE,CAAC;CACvC,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,OAAO,EAAE,eAAe,EAAE,CAAC;IAC3B,OAAO,EAAE,eAAe,EAAE,CAAC;CAC5B,CAAC"}
|