Skip to content

Commit

Permalink
Merge pull request #1017 from mainmatter/doc-ts
Browse files Browse the repository at this point in the history
doc(typescript): add usage example
  • Loading branch information
BobrImperator authored Dec 28, 2024
2 parents 1a9e53c + 55dacba commit d0643d7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
39 changes: 36 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 });

Expand Down
8 changes: 7 additions & 1 deletion packages/ember-cookies/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -28,6 +29,7 @@ export default [
{
plugins: {
ember,
'@typescript-eslint': typescriptEslintPlugin,
},
files: ['**/*.{ts,js}'],

Expand All @@ -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: [
Expand Down
2 changes: 2 additions & 0 deletions packages/ember-cookies/src/services/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export default class CookiesService extends Service {
return this._filterCachedFastBootCookies(mergedFastBootCookies);
}

read(): Record<string, string | undefined>;
read(_name: string): string | undefined;
read(
name?: string,
options: ReadOptions = {}
Expand Down
10 changes: 8 additions & 2 deletions packages/test-app/app/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -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());
Expand All @@ -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 };
}
}

0 comments on commit d0643d7

Please sign in to comment.