diff --git a/CHANGELOG.md b/CHANGELOG.md index 622b8879..d5e5a2ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,9 +13,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - `clearCache()` API method [#122](https://github.com/graphcool/chromeless/pull/122) @joeyvandijk - `scrollToElement()` command and `scrollBeforeClick` constructor option [#15](https://github.com/graphcool/chromeless/issues/15), [#167](https://github.com/graphcool/chromeless/pull/167) @janza - Mocha E2E tests [example](examples/mocha-chai-test-example.js) [#164](https://github.com/graphcool/chromeless/pull/164) @FabioAntunes +- `cookies(name: string)` API method [#183](https://github.com/graphcool/chromeless/pull/183/files) @criticalbh ### Changed - **Breaking:** We renamed `cookiesClear()` to `deleteCookies()`, `cookiesClearAll()` to `clearCookies()` and according to semver should bump the version to 2.0.0, however, just-this-time, we're not going to. [#123](https://github.com/graphcool/chromeless/pull/123) @joeyvandijk +- We renamed `cookiesGet(name: string | query: CookieQuery)` to `cookies(name: string | query: CookieQuery)`, `cookiesGet()` to `cookies()` and `cookiesGetAll()` to `allCookies()` [#183] (https://github.com/graphcool/chromeless/pull/183/files) @criticalbh ### Fixed - Chromeless can now be imported into TypeScript projects with activated `strictNullChecks` compiler option [#154](https://github.com/graphcool/chromeless/pull/154) @clebert diff --git a/README.md b/README.md index fc633edb..4e811af4 100644 --- a/README.md +++ b/README.md @@ -162,10 +162,10 @@ const chromeless = new Chromeless({ - [`screenshot()`](docs/api.md#api-screenshot) - [`pdf(options?: PdfOptions)`](docs/api.md#api-pdf) - [`html()`](docs/api.md#api-html) -- [`cookiesGet()`](docs/api.md#api-cookiesget) -- [`cookiesGet(name: string)`](docs/api.md#api-cookiesget-name) -- [`cookiesGet(query: CookieQuery)`](docs/api.md#api-cookiesget-query) - Not implemented yet -- [`cookiesGetAll()`](docs/api.md#api-cookiesgetall) +- [`cookies()`](docs/api.md#api-cookies) +- [`cookies(name: string)`](docs/api.md#api-cookies-name) +- [`cookies(query: CookieQuery)`](docs/api.md#api-cookies-query) - Not implemented yet +- [`allCookies()`](docs/api.md#api-all-cookies) - [`cookiesSet(name: string, value: string)`](docs/api.md#api-cookiesset) - [`cookiesSet(cookie: Cookie)`](docs/api.md#api-cookiesset-one) - [`cookiesSet(cookies: Cookie[])`](docs/api.md#api-cookiesset-many) diff --git a/docs/api.md b/docs/api.md index d2695777..5688ba0d 100644 --- a/docs/api.md +++ b/docs/api.md @@ -31,10 +31,10 @@ Chromeless provides TypeScript typings. - [`screenshot()`](#api-screenshot) - [`pdf(options?: PdfOptions)`](#api-pdf) - [`html()`](#api-html) -- [`cookiesGet()`](#api-cookiesget) -- [`cookiesGet(name: string)`](#api-cookiesget-name) -- [`cookiesGet(query: CookieQuery)`](#api-cookiesget-query) - Not implemented yet -- [`cookiesGetAll()`](#api-cookiesgetall) +- [`cookies()`](#api-cookies) +- [`cookies(name: string)`](#api-cookies-name) +- [`cookies(query: CookieQuery)`](#api-cookies-query) - Not implemented yet +- [`allCookies()`](#api-all-cookies) - [`cookiesSet(name: string, value: string)`](#api-cookiesset) - [`cookiesSet(cookie: Cookie)`](#api-cookiesset-one) - [`cookiesSet(cookies: Cookie[])`](#api-cookiesset-many) @@ -484,23 +484,23 @@ console.log(html) //

Hello world!

+ -### cookiesGet(): Chromeless +### cookies(): Chromeless Returns all browser cookies for the current URL. __Example__ ```js -await chromeless.cookiesGet() +await chromeless.cookies() ``` --------------------------------------- - + -### cookiesGet(name: string): Chromeless +### cookies(name: string): Chromeless Returns a specific browser cookie by name for the current URL. @@ -510,29 +510,29 @@ __Arguments__ __Example__ ```js -const cookie = await chromeless.cookiesGet('creepyTrackingCookie') +const cookie = await chromeless.cookies('creepyTrackingCookie') ``` --------------------------------------- - + -### cookiesGet(query: CookieQuery) - Not implemented yet +### cookies(query: CookieQuery) - Not implemented yet Not implemented yet --------------------------------------- - + -### cookiesGetAll(): Chromeless +### allCookies(): Chromeless Returns all browser cookies. Nam nom nom. __Example__ ```js -await chromeless.cookiesGetAll() +await chromeless.allCookies() ``` --------------------------------------- diff --git a/src/api.ts b/src/api.ts index 029a99bd..822bc1c2 100644 --- a/src/api.ts +++ b/src/api.ts @@ -236,35 +236,35 @@ export default class Chromeless implements Promise { /** * Get the cookies for the current url */ - cookiesGet(): Chromeless + cookies(): Chromeless /** * Get a specific cookie for the current url * @param name */ - cookiesGet(name: string): Chromeless + cookies(name: string): Chromeless /** * Get a specific cookie by query. Not implemented yet * @param query */ - cookiesGet(query: CookieQuery): Chromeless - cookiesGet( + cookies(query: CookieQuery): Chromeless + cookies( nameOrQuery?: string | CookieQuery, ): Chromeless { - if (typeof nameOrQuery !== 'undefined') { - throw new Error('Querying cookies is not implemented yet') + if (typeof nameOrQuery !== 'undefined' && typeof nameOrQuery !== 'string') { + throw new Error('Querying cookies is not implemented yet') } this.lastReturnPromise = this.queue.process({ - type: 'cookiesGet', + type: 'cookies', nameOrQuery, }) return new Chromeless({}, this) } - cookiesGetAll(): Chromeless { + allCookies(): Chromeless { this.lastReturnPromise = this.queue.process({ - type: 'cookiesGetAll', + type: 'allCookies', }) return new Chromeless({}, this) diff --git a/src/chrome/local-runtime.ts b/src/chrome/local-runtime.ts index c6d82dcd..3536b0f5 100644 --- a/src/chrome/local-runtime.ts +++ b/src/chrome/local-runtime.ts @@ -96,10 +96,10 @@ export default class LocalRuntime { return this.clearCookies() case 'setHtml': return this.setHtml(command.html) - case 'cookiesGet': - return this.cookiesGet(command.nameOrQuery) - case 'cookiesGetAll': - return this.cookiesGetAll() + case 'cookies': + return this.cookies(command.nameOrQuery) + case 'allCookies': + return this.allCookies() case 'cookiesSet': return this.cookiesSet(command.nameOrCookies, command.value) case 'mousedown': @@ -280,11 +280,11 @@ export default class LocalRuntime { this.log(`Typed ${text} in ${selector}`) } - async cookiesGet(nameOrQuery?: string | CookieQuery): Promise { + async cookies(nameOrQuery?: string | CookieQuery): Promise { return await getCookies(this.client, nameOrQuery as string | undefined) } - async cookiesGetAll(): Promise { + async allCookies(): Promise { return await getAllCookies(this.client) } diff --git a/src/types.ts b/src/types.ts index 3e9a7845..88433256 100644 --- a/src/types.ts +++ b/src/types.ts @@ -149,10 +149,10 @@ export type Command = value?: string } | { - type: 'cookiesGetAll' + type: 'allCookies' } | { - type: 'cookiesGet' + type: 'cookies' nameOrQuery?: string | CookieQuery } | { diff --git a/src/util.ts b/src/util.ts index fd9f1b3e..7efc34bd 100644 --- a/src/util.ts +++ b/src/util.ts @@ -324,18 +324,20 @@ export async function getCookies( client: Client, nameOrQuery?: string | Cookie, ): Promise { - if (nameOrQuery) { - throw new Error('Not yet implemented') - } - - const { Network } = client + const {Network} = client const fn = () => location.href - const url = (await evaluate(client, `${fn}`)) as string + const url = await evaluate(client, `${fn}`) as string const result = await Network.getCookies([url]) - return result.cookies + const cookies = result.cookies + + if (typeof nameOrQuery !== 'undefined' && typeof nameOrQuery === 'string') { + const filteredCookies: Cookie[] = cookies.filter(cookie => cookie.name === nameOrQuery) + return filteredCookies + } + return cookies } export async function getAllCookies(client: Client): Promise {