Skip to content

Commit 8bfe0d4

Browse files
committed
Additional type fixes for Cookie Provider
1 parent fe7614a commit 8bfe0d4

File tree

6 files changed

+107
-102
lines changed

6 files changed

+107
-102
lines changed

@types/animations/raf-scheduler.d.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
/**
22
* @typedef {import('./interface.ts').RafScheduler} RafScheduler
3-
* @typedef {import('../interface.ts').ServiceProvider} ServiceProvider
43
*/
54
/**
65
* Service provider that creates a requestAnimationFrame-based scheduler.
7-
* @type {ServiceProvider}
6+
* @type {ng.ServiceProvider}
87
*/
98
export class RafSchedulerProvider {
109
/**
@@ -34,4 +33,3 @@ export class RafSchedulerProvider {
3433
$get(): RafScheduler;
3534
}
3635
export type RafScheduler = import("./interface.ts").RafScheduler;
37-
export type ServiceProvider = import("../interface.ts").ServiceProvider;

@types/namespace.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import {
7676
EntityClass as TEntityClass,
7777
} from "./services/rest/interface.ts";
7878
import { RestService as TRestService } from "./services/rest/rest.js";
79+
import { ServiceProvider as TServiceProvider } from "./interface.ts";
7980
declare global {
8081
interface Function {
8182
$inject?: readonly string[] | undefined;
@@ -148,5 +149,6 @@ declare global {
148149
type RestService<T, ID> = TRestService<T, ID>;
149150
type RestDefinition<T> = TRestDefinition<T>;
150151
type EntityClass<T> = TEntityClass<T>;
152+
type ServiceProvider = TServiceProvider;
151153
}
152154
}

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/**
2+
* Service provider that creates a {@link ng.CookieService $cookie} service.
3+
* @type {ng.ServiceProvider}
4+
*/
5+
export class CookieProvider {
6+
defaults: {};
7+
$get: (
8+
| string
9+
| (($exceptionHandler: ng.ExceptionHandlerService) => CookieService)
10+
)[];
11+
}
112
/**
213
* $cookies service class
314
*
@@ -66,10 +77,3 @@ export class CookieService {
6677
*/
6778
remove(key: string, options?: ng.CookieOptions): void;
6879
}
69-
export class CookieProvider {
70-
defaults: {};
71-
$get: (
72-
| string
73-
| (($exceptionHandler: ng.ExceptionHandlerService) => CookieService)
74-
)[];
75-
}

src/animations/raf-scheduler.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/**
22
* @typedef {import('./interface.ts').RafScheduler} RafScheduler
3-
* @typedef {import('../interface.ts').ServiceProvider} ServiceProvider
43
*/
54

65
/**
76
* Service provider that creates a requestAnimationFrame-based scheduler.
8-
* @type {ServiceProvider}
7+
* @type {ng.ServiceProvider}
98
*/
109
export class RafSchedulerProvider {
1110
constructor() {

src/namespace.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ import {
8080
EntityClass as TEntityClass,
8181
} from "./services/rest/interface.ts";
8282
import { RestService as TRestService } from "./services/rest/rest.js";
83+
import { ServiceProvider as TServiceProvider } from "./interface.ts";
8384

8485
/* ────────────────────────────────────────────────
8586
Runtime global initialization
@@ -170,5 +171,6 @@ declare global {
170171
export type RestService<T, ID> = TRestService<T, ID>;
171172
export type RestDefinition<T> = TRestDefinition<T>;
172173
export type EntityClass<T> = TEntityClass<T>;
174+
export type ServiceProvider = TServiceProvider;
173175
}
174176
}

src/services/cookie/cookie.js

Lines changed: 90 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -10,91 +10,19 @@ import {
1010
} from "../../shared/utils.js";
1111

1212
/**
13-
* @returns {Record<string,string>}
14-
*/
15-
function parseCookies() {
16-
/** @type {Record<string, string>} */
17-
const out = {};
18-
if (!document.cookie) return out;
19-
20-
const parts = document.cookie.split("; ");
21-
for (const part of parts) {
22-
const eq = part.indexOf("=");
23-
if (eq === -1) continue; // skip malformed cookie
24-
const key = decodeURIComponent(part.substring(0, eq));
25-
const val = decodeURIComponent(part.substring(eq + 1));
26-
out[key] = val;
27-
}
28-
return out;
29-
}
30-
31-
/** Utility: stringify options */
32-
/**
33-
*
34-
* @param {ng.CookieOptions} opts
35-
* @returns {string}
13+
* Service provider that creates a {@link ng.CookieService $cookie} service.
14+
* @type {ng.ServiceProvider}
3615
*/
37-
/**
38-
* Build cookie options string from an options object.
39-
* Safely validates types for path, domain, expires, secure, and samesite.
40-
*
41-
* @param {ng.CookieOptions} opts
42-
* @returns {string}
43-
* @throws {TypeError} if any of options are invalid
44-
*/
45-
function buildOptions(opts = {}) {
46-
const parts = [];
47-
48-
// Path
49-
if (isDefined(opts.path)) {
50-
if (!isString(opts.path)) throw new TypeError(`badarg:path ${opts.path}`);
51-
parts.push(`path=${opts.path}`);
52-
}
53-
54-
// Domain
55-
if (isDefined(opts.domain)) {
56-
if (!isString(opts.domain))
57-
throw new TypeError(`badarg:domain ${opts.domain}`);
58-
parts.push(`domain=${opts.domain}`);
59-
}
60-
61-
// Expires
62-
if (opts.expires != null) {
63-
let expDate;
64-
65-
if (opts.expires instanceof Date) {
66-
expDate = opts.expires;
67-
} else if (isNumber(opts.expires) || isString(opts.expires)) {
68-
expDate = new Date(opts.expires);
69-
} else {
70-
throw new TypeError(`badarg:expires ${String(opts.expires)}`);
71-
}
72-
73-
if (isNaN(expDate.getTime())) {
74-
throw new TypeError(`badarg:expires ${String(opts.expires)}`);
75-
}
76-
77-
parts.push(`expires=${expDate.toUTCString()}`);
78-
}
79-
80-
// Secure
81-
if (opts.secure) {
82-
parts.push("secure");
83-
}
84-
85-
// SameSite
86-
if (isDefined(opts.samesite)) {
87-
if (!isString(opts.samesite))
88-
throw new TypeError(`badarg:samesite ${opts.samesite}`);
89-
const s = opts.samesite.toLowerCase();
90-
if (!["lax", "strict", "none"].includes(s)) {
91-
throw new TypeError(`badarg:samesite ${opts.samesite}`);
92-
}
93-
parts.push(`samesite=${s}`);
16+
export class CookieProvider {
17+
constructor() {
18+
this.defaults = {};
9419
}
9520

96-
// Join all parts with semicolons
97-
return parts.length ? ";" + parts.join(";") : "";
21+
$get = [
22+
$injectTokens.$exceptionHandler,
23+
/** @param {ng.ExceptionHandlerService} $exceptionHandler */
24+
($exceptionHandler) => new CookieService(this.defaults, $exceptionHandler),
25+
];
9826
}
9927

10028
/**
@@ -221,14 +149,86 @@ export class CookieService {
221149
}
222150
}
223151

224-
export class CookieProvider {
225-
constructor() {
226-
this.defaults = {};
152+
/*----------Helpers----------*/
153+
154+
/**
155+
* @returns {Record<string,string>}
156+
*/
157+
function parseCookies() {
158+
/** @type {Record<string, string>} */
159+
const out = {};
160+
if (!document.cookie) return out;
161+
162+
const parts = document.cookie.split("; ");
163+
for (const part of parts) {
164+
const eq = part.indexOf("=");
165+
if (eq === -1) continue; // skip malformed cookie
166+
const key = decodeURIComponent(part.substring(0, eq));
167+
const val = decodeURIComponent(part.substring(eq + 1));
168+
out[key] = val;
169+
}
170+
return out;
171+
}
172+
173+
/**
174+
* Build cookie options string from an options object.
175+
* Safely validates types for path, domain, expires, secure, and samesite.
176+
*
177+
* @param {ng.CookieOptions} opts
178+
* @returns {string}
179+
* @throws {TypeError} if any of options are invalid
180+
*/
181+
function buildOptions(opts = {}) {
182+
const parts = [];
183+
184+
// Path
185+
if (isDefined(opts.path)) {
186+
if (!isString(opts.path)) throw new TypeError(`badarg:path ${opts.path}`);
187+
parts.push(`path=${opts.path}`);
227188
}
228189

229-
$get = [
230-
$injectTokens.$exceptionHandler,
231-
/** @param {ng.ExceptionHandlerService} $exceptionHandler */
232-
($exceptionHandler) => new CookieService(this.defaults, $exceptionHandler),
233-
];
190+
// Domain
191+
if (isDefined(opts.domain)) {
192+
if (!isString(opts.domain))
193+
throw new TypeError(`badarg:domain ${opts.domain}`);
194+
parts.push(`domain=${opts.domain}`);
195+
}
196+
197+
// Expires
198+
if (opts.expires != null) {
199+
let expDate;
200+
201+
if (opts.expires instanceof Date) {
202+
expDate = opts.expires;
203+
} else if (isNumber(opts.expires) || isString(opts.expires)) {
204+
expDate = new Date(opts.expires);
205+
} else {
206+
throw new TypeError(`badarg:expires ${String(opts.expires)}`);
207+
}
208+
209+
if (isNaN(expDate.getTime())) {
210+
throw new TypeError(`badarg:expires ${String(opts.expires)}`);
211+
}
212+
213+
parts.push(`expires=${expDate.toUTCString()}`);
214+
}
215+
216+
// Secure
217+
if (opts.secure) {
218+
parts.push("secure");
219+
}
220+
221+
// SameSite
222+
if (isDefined(opts.samesite)) {
223+
if (!isString(opts.samesite))
224+
throw new TypeError(`badarg:samesite ${opts.samesite}`);
225+
const s = opts.samesite.toLowerCase();
226+
if (!["lax", "strict", "none"].includes(s)) {
227+
throw new TypeError(`badarg:samesite ${opts.samesite}`);
228+
}
229+
parts.push(`samesite=${s}`);
230+
}
231+
232+
// Join all parts with semicolons
233+
return parts.length ? ";" + parts.join(";") : "";
234234
}

0 commit comments

Comments
 (0)