UNPKG

1.82 kBPlain TextView Raw
1// Copyright (c) .NET Foundation. All rights reserved.
2// Licensed under the MIT License.
3
4import { Cookie } from '@azure/functions';
5import { RpcHttpCookie, RpcHttpCookieSameSite } from '@azure/functions-core';
6import { toNullableBool, toNullableDouble, toNullableString, toNullableTimestamp, toRpcString } from './toRpcNullable';
7
8/**
9 * From RFC specifications for 'Set-Cookie' response header: https://www.rfc-editor.org/rfc/rfc6265.txt
10 * @param inputCookie
11 */
12export function toRpcHttpCookie(inputCookie: Cookie): RpcHttpCookie {
13 // Resolve RpcHttpCookie.SameSite enum, a one-off
14 let rpcSameSite: RpcHttpCookieSameSite = 'none';
15 if (inputCookie && inputCookie.sameSite) {
16 const sameSite = inputCookie.sameSite.toLocaleLowerCase();
17 if (sameSite === 'lax') {
18 rpcSameSite = 'lax';
19 } else if (sameSite === 'strict') {
20 rpcSameSite = 'strict';
21 } else if (sameSite === 'none') {
22 rpcSameSite = 'explicitNone';
23 }
24 }
25
26 const rpcCookie: RpcHttpCookie = {
27 name: inputCookie && toRpcString(inputCookie.name, 'cookie.name'),
28 value: inputCookie && toRpcString(inputCookie.value, 'cookie.value'),
29 domain: toNullableString(inputCookie && inputCookie.domain, 'cookie.domain'),
30 path: toNullableString(inputCookie && inputCookie.path, 'cookie.path'),
31 expires: toNullableTimestamp(inputCookie && inputCookie.expires, 'cookie.expires'),
32 secure: toNullableBool(inputCookie && inputCookie.secure, 'cookie.secure'),
33 httpOnly: toNullableBool(inputCookie && inputCookie.httpOnly, 'cookie.httpOnly'),
34 sameSite: rpcSameSite,
35 maxAge: toNullableDouble(inputCookie && inputCookie.maxAge, 'cookie.maxAge'),
36 };
37
38 return rpcCookie;
39}