Skip to content

Commit 45f87e9

Browse files
committed
Cookie store and bring back cookie service
1 parent bc25020 commit 45f87e9

File tree

14 files changed

+324
-6
lines changed

14 files changed

+324
-6
lines changed

@types/core/di/ng-module/ng-module.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ export class NgModule {
183183
* @returns {NgModule}
184184
*/
185185
local(name: string, ctor: Function): NgModule;
186+
/**
187+
* @param {string} name
188+
* @param {Function} ctor
189+
* @returns {NgModule}
190+
*/
191+
cookie(name: string, ctor: Function): NgModule;
186192
/**
187193
* @param {string} name
188194
* @param {Function} ctor

@types/namespace.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ import { Location as TLocationService } from "./services/location/location.js";
6666
import { AnimateService as TAnimateService } from "./animations/interface.ts";
6767
import { StorageBackend as TStorageBackend } from "./services/storage/interface.ts";
6868
import { StreamConnectionConfig as TStreamConnectionConfig } from "./services/stream/interface.ts";
69+
import { CookieService as TCookieService } from "./services/cookie/cookie.js";
70+
import {
71+
CookieStoreOptions as TCookieStoreOptions,
72+
CookieOptions as TCookieOptions,
73+
} from "./services/cookie/interface.ts";
6974
declare global {
7075
interface Function {
7176
$inject?: readonly string[] | undefined;
@@ -100,6 +105,7 @@ declare global {
100105
type AnimateService = TAnimateService;
101106
type CompileService = TCompileFn;
102107
type ControllerService = TControllerService;
108+
type CookieService = TCookieService;
103109
type ExceptionHandlerService = TErrorHandler;
104110
type FilterService = TFilterFactory;
105111
type HttpParamSerializerSerService = THttpParamSerializer;
@@ -132,5 +138,7 @@ declare global {
132138
> = TInjectable<T>;
133139
type StorageBackend = TStorageBackend;
134140
type StreamConnectionConfig = TStreamConnectionConfig;
141+
type CookieOptions = TCookieOptions;
142+
type CookieStoreOptions = TCookieStoreOptions;
135143
}
136144
}

@types/services/cookie/cookie.d.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* $cookies service class
3+
*
4+
* Provides high-level APIs for interacting with browser cookies:
5+
* - Raw get/set/remove
6+
* - JSON serialization helpers
7+
* - Global defaults supplied by $cookiesProvider
8+
*/
9+
export class CookieService {
10+
/**
11+
* @param {ng.CookieOptions} defaults
12+
* Default cookie attributes defined by `$cookiesProvider.defaults`.
13+
*/
14+
constructor(defaults: ng.CookieOptions);
15+
/** @type {CookieOptions} */
16+
defaults: CookieOptions;
17+
/**
18+
* Retrieves a raw cookie value.
19+
*
20+
* @param {string} key
21+
* @returns {string|null}
22+
*/
23+
get(key: string): string | null;
24+
/**
25+
* Retrieves a cookie and deserializes its JSON content.
26+
*
27+
* @template T
28+
* @param {string} key
29+
* @returns {T|null}
30+
*/
31+
getObject<T>(key: string): T | null;
32+
/**
33+
* Returns an object containing all raw cookies.
34+
*
35+
* @returns {Record<string, string>}
36+
*/
37+
getAll(): Record<string, string>;
38+
/**
39+
* Sets a raw cookie value.
40+
*
41+
* @param {string} key
42+
* @param {string} value
43+
* @param {CookieOptions} [options]
44+
*/
45+
put(key: string, value: string, options?: CookieOptions): void;
46+
/**
47+
* Serializes an object as JSON and stores it as a cookie.
48+
*
49+
* @param {string} key
50+
* @param {any} value
51+
* @param {CookieOptions} [options]
52+
*/
53+
putObject(key: string, value: any, options?: CookieOptions): void;
54+
/**
55+
* Removes a cookie by setting an expired date.
56+
*
57+
* @param {string} key
58+
* @param {CookieOptions} [options]
59+
*/
60+
remove(key: string, options?: CookieOptions): void;
61+
}
62+
export class CookieProvider {
63+
defaults: {};
64+
$get(): CookieService;
65+
}
66+
export type CookieOptions = import("./interface.ts").CookieOptions;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export interface CookieOptions {
2+
path?: string;
3+
domain?: string;
4+
expires?: Date | string | number;
5+
secure?: boolean;
6+
samesite?: "Lax" | "Strict" | "None";
7+
}
8+
export interface CookieStoreOptions {
9+
serialize?: (value: any) => string;
10+
deserialize?: (text: string) => any;
11+
cookie?: CookieOptions;
12+
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
2-
<script src="https://cdn.jsdelivr.net/npm/@angular-wave/[email protected]/dist/angular-ts.umd.js"></script>
3-
1+
<script src="https://cdn.jsdelivr.net/npm/@angular-wave/[email protected]/dist/angular-ts.umd.js"></script>

src/core/di/injector.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import { INJECTOR_LITERAL } from "./ng-module/ng-module.js";
1313
import { InjectorService, ProviderInjector } from "./internal-injector.js";
1414
import { createPersistentProxy } from "../../services/storage/storage.js";
15+
import { $injectTokens } from "../../injection-tokens";
1516

1617
const $injectorMinErr = minErr(INJECTOR_LITERAL);
1718
const providerSuffix = "Provider";
@@ -38,6 +39,7 @@ export function createInjector(modulesToLoad, strictDi = false) {
3839
session: supportObject(session),
3940
local: supportObject(local),
4041
store: supportObject(store),
42+
cookie: supportObject(cookie),
4143
decorator,
4244
},
4345
};
@@ -195,6 +197,46 @@ export function createInjector(modulesToLoad, strictDi = false) {
195197
});
196198
}
197199

200+
/**
201+
* Registers a cookie-persistent service.
202+
*
203+
* @param {string} name
204+
* @param {Function} ctor
205+
* @param {ng.CookieStoreOptions} [options]
206+
*/
207+
function cookie(name, ctor, options = {}) {
208+
return provider(name, {
209+
$get: [
210+
$injectTokens.$injector,
211+
($injector) => {
212+
const instance = $injector.instantiate(ctor);
213+
const $cookie = $injector.get($injectTokens.$cookie);
214+
const serialize = options.serialize ?? JSON.stringify;
215+
const deserialize = options.deserialize ?? JSON.parse;
216+
const cookieOpts = options.cookie ?? {};
217+
218+
return createPersistentProxy(instance, name, {
219+
getItem: (key) => {
220+
const raw = $cookie.get(key);
221+
return raw == null ? null : raw;
222+
},
223+
224+
setItem: (key, value) => {
225+
$cookie.put(key, value, cookieOpts);
226+
},
227+
228+
removeItem: (key) => {
229+
$cookie.remove(key, cookieOpts);
230+
},
231+
232+
serialize,
233+
deserialize,
234+
});
235+
},
236+
],
237+
});
238+
}
239+
198240
/**
199241
* Registers a service persisted in a custom storage
200242
*

src/core/di/ng-module/ng-module.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,19 @@ export class NgModule {
327327
return this;
328328
}
329329

330+
/**
331+
* @param {string} name
332+
* @param {Function} ctor
333+
* @returns {NgModule}
334+
*/
335+
cookie(name, ctor) {
336+
if (ctor && isFunction(ctor)) {
337+
ctor["$$moduleName"] = name;
338+
}
339+
this.invokeQueue.push([$t.$provide, "cookie", [name, ctor]]);
340+
return this;
341+
}
342+
330343
/**
331344
* @param {string} name
332345
* @param {Function} ctor

src/injection-tokens.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export const $injectTokens = Object.freeze({
3232
$animateCss: "$animateCss",
3333
$aria: "$aria",
3434
$compile: "$compile",
35+
$cookie: "$cookie",
3536
$controller: "$controller",
3637
$document: "$document",
3738
$eventBus: "$eventBus",

src/namespace.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ import { Location as TLocationService } from "./services/location/location.js";
7070
import { AnimateService as TAnimateService } from "./animations/interface.ts";
7171
import { StorageBackend as TStorageBackend } from "./services/storage/interface.ts";
7272
import { StreamConnectionConfig as TStreamConnectionConfig } from "./services/stream/interface.ts";
73+
import { CookieService as TCookieService } from "./services/cookie/cookie.js";
74+
import {
75+
CookieStoreOptions as TCookieStoreOptions,
76+
CookieOptions as TCookieOptions,
77+
} from "./services/cookie/interface.ts";
7378

7479
/* ────────────────────────────────────────────────
7580
Runtime global initialization
@@ -120,6 +125,7 @@ declare global {
120125
export type AnimateService = TAnimateService;
121126
export type CompileService = TCompileFn;
122127
export type ControllerService = TControllerService;
128+
export type CookieService = TCookieService;
123129
export type ExceptionHandlerService = TErrorHandler;
124130
export type FilterService = TFilterFactory;
125131
export type HttpParamSerializerSerService = THttpParamSerializer;
@@ -154,5 +160,7 @@ declare global {
154160
> = TInjectable<T>;
155161
export type StorageBackend = TStorageBackend;
156162
export type StreamConnectionConfig = TStreamConnectionConfig;
163+
export type CookieOptions = TCookieOptions;
164+
export type CookieStoreOptions = TCookieStoreOptions;
157165
}
158166
}

src/ng.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ import { ngViewportDirective } from "./directive/viewport/viewport.js";
136136
import { ngWorkerDirective } from "./directive/worker/worker.js";
137137
import { ngWasmDirective } from "./directive/wasm/wasm.js";
138138
import { ngScopeDirective } from "./directive/scope/scope.js";
139+
import { CookieProvider } from "./services/cookie/cookie.js";
139140

140141
/**
141142
* Initializes core `ng` module.
@@ -260,6 +261,7 @@ export function registerNgModule(angular) {
260261
$$animateCache: AnimateCacheProvider,
261262
$$animateQueue: AnimateQueueProvider,
262263
$controller: ControllerProvider,
264+
$cookie: CookieProvider,
263265
$exceptionHandler: ExceptionHandlerProvider,
264266
$filter: FilterProvider,
265267
$interpolate: InterpolateProvider,

0 commit comments

Comments
 (0)