diff --git a/README.md b/README.md index 23d2e394..6204aa15 100644 --- a/README.md +++ b/README.md @@ -22,12 +22,45 @@ Install `ember-cookies` with ## Example Usage +### Typescript + +```ts +// app/controllers/index.ts +import { inject as service } from '@ember/service'; +import Controller from '@ember/controller'; +import CookiesService from 'ember-cookies/services/cookies'; + +type Cookie = { name: string; value?: string }; + +export default class IndexController extends Controller { + @service cookies!: CookiesService; + + get allCookies(): Cookie[] { + this.cookies.write('now', new Date().getTime()); + + const cookies = this.cookies.read(); + return Object.keys(cookies).reduce((acc, key) => { + let value = cookies[key]; + acc.push({ name: key, value }); + + return acc; + }, [] as Cookie[]); + } + + get singleCookie(): Cookie { + const cookie = this.cookies.read('now'); + return { name: 'now', value: cookie }; + } +} +``` + +### Javascript + ```js -// app/controllers/application.js +// app/controllers/index.js import Controller from '@ember/controller'; import { inject as service } from '@ember/service'; import { computed } from '@ember/object'; -const { keys } = Object; export default class ApplicationController extends Controller { @service cookies; @@ -38,7 +71,7 @@ export default class ApplicationController extends Controller { cookieService.write('now', new Date().getTime()); let cookies = cookieService.read(); - return keys(cookies).reduce((acc, key) => { + return Object.keys(cookies).reduce((acc, key) => { let value = cookies[key]; acc.push({ name: key, value }); diff --git a/packages/ember-cookies/eslint.config.mjs b/packages/ember-cookies/eslint.config.mjs index 4af86c6a..a0599c80 100644 --- a/packages/ember-cookies/eslint.config.mjs +++ b/packages/ember-cookies/eslint.config.mjs @@ -2,6 +2,7 @@ import ember from 'eslint-plugin-ember'; import prettier from 'eslint-plugin-prettier/recommended'; import globals from 'globals'; import typescriptParser from '@typescript-eslint/parser'; +import typescriptEslintPlugin from '@typescript-eslint/eslint-plugin'; import n from 'eslint-plugin-n'; import js from '@eslint/js'; @@ -28,6 +29,7 @@ export default [ { plugins: { ember, + '@typescript-eslint': typescriptEslintPlugin, }, files: ['**/*.{ts,js}'], @@ -47,7 +49,11 @@ export default [ }, }, - rules: {}, + rules: { + 'no-dupe-class-members': 'off', + '@typescript-eslint/no-dupe-class-members': 'error', + 'no-unused-vars': ['error', { argsIgnorePattern: '^_' }], + }, }, { files: [ diff --git a/packages/ember-cookies/src/services/cookies.ts b/packages/ember-cookies/src/services/cookies.ts index bd93ce28..d301dff8 100644 --- a/packages/ember-cookies/src/services/cookies.ts +++ b/packages/ember-cookies/src/services/cookies.ts @@ -97,6 +97,8 @@ export default class CookiesService extends Service { return this._filterCachedFastBootCookies(mergedFastBootCookies); } + read(): Record; + read(_name: string): string | undefined; read( name?: string, options: ReadOptions = {} diff --git a/packages/test-app/app/controllers/index.ts b/packages/test-app/app/controllers/index.ts index 8c0373dc..92fe84d9 100644 --- a/packages/test-app/app/controllers/index.ts +++ b/packages/test-app/app/controllers/index.ts @@ -1,10 +1,11 @@ import { inject as service } from '@ember/service'; import Controller from '@ember/controller'; +import CookiesService from 'ember-cookies/services/cookies'; -type Cookie = { name: string; value: any }; +type Cookie = { name: string; value?: string }; export default class IndexController extends Controller { - @service cookies!: any; + @service cookies!: CookiesService; get allCookies(): Cookie[] { this.cookies.write('now', new Date().getTime()); @@ -17,4 +18,9 @@ export default class IndexController extends Controller { return acc; }, [] as Cookie[]); } + + get singleCookie(): Cookie { + const cookie = this.cookies.read('now'); + return { name: 'now', value: cookie }; + } }