Skip to content

Commit

Permalink
doc(typescript): add usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
BobrImperator committed Dec 28, 2024
1 parent 1a9e53c commit d520437
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 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: any };

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
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
8 changes: 7 additions & 1 deletion 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 };

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 d520437

Please sign in to comment.