( name: string, value: string, options?: Cookie["options"] | undefined )
| 328 | * @category constructors |
| 329 | */ |
| 330 | export function makeCookie( |
| 331 | name: string, |
| 332 | value: string, |
| 333 | options?: Cookie["options"] | undefined |
| 334 | ): Either.Either<Cookie, CookiesError> { |
| 335 | if (!cookieNameRegExp.test(name)) { |
| 336 | return Either.left(new CookiesError({ reason: "InvalidName" })) |
| 337 | } |
| 338 | const encodedValue = encodeURIComponent(value) |
| 339 | if (encodedValue && !cookieValueRegExp.test(encodedValue)) { |
| 340 | return Either.left(new CookiesError({ reason: "InvalidValue" })) |
| 341 | } |
| 342 | |
| 343 | if (options !== undefined) { |
| 344 | if (options.domain !== undefined && !cookieDomainRegExp.test(options.domain)) { |
| 345 | return Either.left(new CookiesError({ reason: "InvalidDomain" })) |
| 346 | } |
| 347 | |
| 348 | if (options.path !== undefined && !cookiePathRegExp.test(options.path)) { |
| 349 | return Either.left(new CookiesError({ reason: "InvalidPath" })) |
| 350 | } |
| 351 | |
| 352 | if (options.maxAge !== undefined && !Duration.isFinite(Duration.decode(options.maxAge))) { |
| 353 | return Either.left(new CookiesError({ reason: "InfinityMaxAge" })) |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | return Either.right(Object.assign(Object.create(CookieProto), { |
| 358 | name, |
| 359 | value, |
| 360 | valueEncoded: encodedValue, |
| 361 | options |
| 362 | })) |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Create a new cookie, throwing an error if invalid |
no test coverage detected