Skip to content

Commit 52945a3

Browse files
authored
Remove enums from core api (#6)
1 parent 258f042 commit 52945a3

12 files changed

+72
-176
lines changed

src/Context.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
Logger,
1111
TraceContext,
1212
} from '@azure/functions';
13-
import { RpcInvocationRequest, RpcLog, RpcParameterBinding } from '@azure/functions-core';
13+
import { RpcInvocationRequest, RpcLogLevel, RpcParameterBinding } from '@azure/functions-core';
1414
import { v4 as uuid } from 'uuid';
1515
import {
1616
convertKeysToCamelCase,
@@ -107,11 +107,11 @@ class InvocationContext implements Context {
107107
this.bindings = {};
108108

109109
// Log message that is tied to function invocation
110-
this.log = Object.assign((...args: any[]) => userLogCallback(RpcLog.Level.Information, ...args), {
111-
error: (...args: any[]) => userLogCallback(RpcLog.Level.Error, ...args),
112-
warn: (...args: any[]) => userLogCallback(RpcLog.Level.Warning, ...args),
113-
info: (...args: any[]) => userLogCallback(RpcLog.Level.Information, ...args),
114-
verbose: (...args: any[]) => userLogCallback(RpcLog.Level.Trace, ...args),
110+
this.log = Object.assign((...args: any[]) => userLogCallback('information', ...args), {
111+
error: (...args: any[]) => userLogCallback('error', ...args),
112+
warn: (...args: any[]) => userLogCallback('warning', ...args),
113+
info: (...args: any[]) => userLogCallback('information', ...args),
114+
verbose: (...args: any[]) => userLogCallback('trace', ...args),
115115
});
116116

117117
this.bindingData = getNormalizedBindingData(request);
@@ -130,7 +130,7 @@ export interface InvocationResult {
130130

131131
export type DoneCallback = (err?: unknown, result?: any) => void;
132132

133-
export type UserLogCallback = (level: RpcLog.Level, ...args: any[]) => void;
133+
export type UserLogCallback = (level: RpcLogLevel, ...args: any[]) => void;
134134

135135
export interface Dict<T> {
136136
[key: string]: T;

src/FunctionInfo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class FunctionInfo {
3232

3333
// determine output bindings & assign rpc converter (http has quirks)
3434
Object.keys(bindings)
35-
.filter((name) => bindings[name].direction !== RpcBindingInfo.Direction.in)
35+
.filter((name) => bindings[name].direction !== 'in')
3636
.forEach((name) => {
3737
const type = bindings[name].type;
3838
if (type && type.toLowerCase() === 'http') {

src/InvocationModel.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
CoreInvocationContext,
88
InvocationArguments,
99
RpcInvocationResponse,
10-
RpcLog,
10+
RpcLogCategory,
11+
RpcLogLevel,
1112
RpcParameterBinding,
1213
} from '@azure/functions-core';
1314
import { format } from 'util';
@@ -35,7 +36,7 @@ export class InvocationModel implements coreTypes.InvocationModel {
3536
const { context, inputs } = CreateContextAndInputs(
3637
this.#funcInfo,
3738
this.#coreCtx.request,
38-
(level: RpcLog.Level, ...args: any[]) => this.#userLog(level, ...args),
39+
(level: RpcLogLevel, ...args: any[]) => this.#userLog(level, ...args),
3940
this.#doneEmitter
4041
);
4142
return { context, inputs };
@@ -142,31 +143,31 @@ export class InvocationModel implements coreTypes.InvocationModel {
142143
return response;
143144
}
144145

145-
#log(level: RpcLog.Level, logCategory: RpcLog.RpcLogCategory, ...args: any[]): void {
146+
#log(level: RpcLogLevel, logCategory: RpcLogCategory, ...args: any[]): void {
146147
this.#coreCtx.log(level, logCategory, format.apply(null, <[any, any[]]>args));
147148
}
148149

149-
#systemLog(level: RpcLog.Level, ...args: any[]) {
150-
this.#log(level, RpcLog.RpcLogCategory.System, ...args);
150+
#systemLog(level: RpcLogLevel, ...args: any[]) {
151+
this.#log(level, 'system', ...args);
151152
}
152153

153-
#userLog(level: RpcLog.Level, ...args: any[]): void {
154+
#userLog(level: RpcLogLevel, ...args: any[]): void {
154155
if (this.#isDone && this.#coreCtx.state !== 'postInvocationHooks') {
155156
let badAsyncMsg =
156157
"Warning: Unexpected call to 'log' on the context object after function execution has completed. Please check for asynchronous calls that are not awaited or calls to 'done' made before function execution completes. ";
157158
badAsyncMsg += `Function name: ${this.#funcInfo.name}. Invocation Id: ${this.#coreCtx.invocationId}. `;
158159
badAsyncMsg += `Learn more: ${asyncDoneLearnMoreLink}`;
159-
this.#systemLog(RpcLog.Level.Warning, badAsyncMsg);
160+
this.#systemLog('warning', badAsyncMsg);
160161
}
161-
this.#log(level, RpcLog.RpcLogCategory.User, ...args);
162+
this.#log(level, 'user', ...args);
162163
}
163164

164165
#onDone(): void {
165166
if (this.#isDone) {
166167
const message = this.#resultIsPromise
167168
? `Error: Choose either to return a promise or call 'done'. Do not use both in your script. Learn more: ${asyncDoneLearnMoreLink}`
168169
: "Error: 'done' has already been called. Please check your script for extraneous calls to 'done'.";
169-
this.#systemLog(RpcLog.Level.Error, message);
170+
this.#systemLog('error', message);
170171
}
171172
this.#isDone = true;
172173
}

src/converters/BindingConverters.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
// Licensed under the MIT License.
33

44
import { BindingDefinition, ContextBindingData } from '@azure/functions';
5-
import { RpcBindingInfo, RpcInvocationRequest } from '@azure/functions-core';
5+
import { RpcInvocationRequest } from '@azure/functions-core';
66
import { FunctionInfo } from '../FunctionInfo';
77
import { fromTypedData } from './RpcConverters';
88

9-
type BindingDirection = 'in' | 'out' | 'inout' | undefined;
10-
119
export function getBindingDefinitions(info: FunctionInfo): BindingDefinition[] {
1210
const bindings = info.bindings;
1311
if (!bindings) {
@@ -18,7 +16,7 @@ export function getBindingDefinitions(info: FunctionInfo): BindingDefinition[] {
1816
return {
1917
name: name,
2018
type: bindings[name].type || '',
21-
direction: getDirectionName(bindings[name].direction),
19+
direction: bindings[name].direction || undefined,
2220
};
2321
});
2422
}
@@ -35,15 +33,6 @@ export function getNormalizedBindingData(request: RpcInvocationRequest): Context
3533
return bindingData;
3634
}
3735

38-
function getDirectionName(direction: RpcBindingInfo.Direction | null | undefined): BindingDirection {
39-
const directionName = Object.keys(RpcBindingInfo.Direction).find((k) => RpcBindingInfo.Direction[k] === direction);
40-
return isBindingDirection(directionName) ? (directionName as BindingDirection) : undefined;
41-
}
42-
43-
function isBindingDirection(input: string | undefined): boolean {
44-
return input == 'in' || input == 'out' || input == 'inout';
45-
}
46-
4736
// Recursively convert keys of objects to camel case
4837
export function convertKeysToCamelCase(obj: any) {
4938
const output = {};

src/converters/RpcHttpConverters.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
// Licensed under the MIT License.
33

44
import { Cookie } from '@azure/functions';
5-
import { RpcHttpCookie, RpcHttpData, RpcNullableString, RpcTypedData } from '@azure/functions-core';
5+
import {
6+
RpcHttpCookie,
7+
RpcHttpCookieSameSite,
8+
RpcHttpData,
9+
RpcNullableString,
10+
RpcTypedData,
11+
} from '@azure/functions-core';
612
import { Dict } from '../Context';
713
import {
814
fromTypedData,
@@ -101,15 +107,15 @@ export function toRpcHttpCookieList(inputCookies: Cookie[]): RpcHttpCookie[] {
101107
*/
102108
function toRpcHttpCookie(inputCookie: Cookie): RpcHttpCookie {
103109
// Resolve RpcHttpCookie.SameSite enum, a one-off
104-
let rpcSameSite: RpcHttpCookie.SameSite = RpcHttpCookie.SameSite.None;
110+
let rpcSameSite: RpcHttpCookieSameSite = 'none';
105111
if (inputCookie && inputCookie.sameSite) {
106112
const sameSite = inputCookie.sameSite.toLocaleLowerCase();
107113
if (sameSite === 'lax') {
108-
rpcSameSite = RpcHttpCookie.SameSite.Lax;
114+
rpcSameSite = 'lax';
109115
} else if (sameSite === 'strict') {
110-
rpcSameSite = RpcHttpCookie.SameSite.Strict;
116+
rpcSameSite = 'strict';
111117
} else if (sameSite === 'none') {
112-
rpcSameSite = RpcHttpCookie.SameSite.ExplicitNone;
118+
rpcSameSite = 'explicitNone';
113119
}
114120
}
115121

test/Context.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ describe('Context', () => {
4444
bindings: {
4545
myTimer: {
4646
type: 'timerTrigger',
47-
direction: 0,
48-
dataType: 0,
47+
direction: 'in',
48+
dataType: 'undefined',
4949
},
5050
},
5151
});
@@ -70,8 +70,8 @@ describe('Context', () => {
7070
bindings: {
7171
req: {
7272
type: 'http',
73-
direction: 0,
74-
dataType: 1,
73+
direction: 'in',
74+
dataType: 'string',
7575
},
7676
},
7777
});
@@ -104,8 +104,8 @@ describe('Context', () => {
104104
bindings: {
105105
req: {
106106
type: 'http',
107-
direction: 0,
108-
dataType: 1,
107+
direction: 'in',
108+
dataType: 'string',
109109
},
110110
},
111111
});
@@ -157,8 +157,8 @@ describe('Context', () => {
157157
bindings: {
158158
req: {
159159
type: 'http',
160-
direction: 0,
161-
dataType: 1,
160+
direction: 'in',
161+
dataType: 'string',
162162
},
163163
},
164164
});
@@ -201,8 +201,8 @@ describe('Context', () => {
201201
bindings: {
202202
req: {
203203
type: 'http',
204-
direction: 0,
205-
dataType: 1,
204+
direction: 'in',
205+
dataType: 'string',
206206
},
207207
},
208208
});

test/FunctionInfo.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ describe('FunctionInfo', () => {
1313
bindings: {
1414
req: {
1515
type: 'httpTrigger',
16-
direction: 0,
17-
dataType: 1,
16+
direction: 'in',
17+
dataType: 'string',
1818
},
1919
$return: {
2020
type: 'http',
21-
direction: 1,
22-
dataType: 1,
21+
direction: 'out',
22+
dataType: 'string',
2323
},
2424
},
2525
};
@@ -33,8 +33,8 @@ describe('FunctionInfo', () => {
3333
bindings: {
3434
req: {
3535
type: 'httpTrigger',
36-
direction: 0,
37-
dataType: 1,
36+
direction: 'in',
37+
dataType: 'string',
3838
},
3939
},
4040
};
@@ -49,13 +49,13 @@ describe('FunctionInfo', () => {
4949
bindings: {
5050
input: {
5151
type: 'queue',
52-
direction: 0,
53-
dataType: 1,
52+
direction: 'in',
53+
dataType: 'string',
5454
},
5555
$return: {
5656
type: 'queue',
57-
direction: 1,
58-
dataType: 1,
57+
direction: 'out',
58+
dataType: 'string',
5959
},
6060
},
6161
};

test/converters/BindingConverters.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
import { RpcBindingInfo, RpcFunctionMetadata, RpcInvocationRequest, RpcTypedData } from '@azure/functions-core';
4+
import { RpcFunctionMetadata, RpcInvocationRequest, RpcTypedData } from '@azure/functions-core';
55
import { expect } from 'chai';
66
import { fromString } from 'long';
77
import 'mocha';
@@ -118,15 +118,15 @@ describe('Binding Converters', () => {
118118
bindings: {
119119
req: {
120120
type: 'httpTrigger',
121-
direction: RpcBindingInfo.Direction.in,
121+
direction: 'in',
122122
},
123123
res: {
124124
type: 'http',
125-
direction: RpcBindingInfo.Direction.out,
125+
direction: 'out',
126126
},
127127
firstQueueOutput: {
128128
type: 'queue',
129-
direction: RpcBindingInfo.Direction.out,
129+
direction: 'out',
130130
},
131131
noDirection: {
132132
type: 'queue',

test/converters/RpcHttpConverters.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
import { Cookie } from '@azure/functions';
5-
import { RpcHttpCookie } from '@azure/functions-core';
65
import { expect } from 'chai';
76
import 'mocha';
87
import { toRpcHttp, toRpcHttpCookieList } from '../../src/converters/RpcHttpConverters';
@@ -69,16 +68,16 @@ describe('Rpc Converters', () => {
6968

7069
const rpcCookies = toRpcHttpCookieList(cookieInputs);
7170
expect(rpcCookies[0].name).to.equal('none-cookie');
72-
expect(rpcCookies[0].sameSite).to.equal(RpcHttpCookie.SameSite.ExplicitNone);
71+
expect(rpcCookies[0].sameSite).to.equal('explicitNone');
7372

7473
expect(rpcCookies[1].name).to.equal('lax-cookie');
75-
expect(rpcCookies[1].sameSite).to.equal(RpcHttpCookie.SameSite.Lax);
74+
expect(rpcCookies[1].sameSite).to.equal('lax');
7675

7776
expect(rpcCookies[2].name).to.equal('strict-cookie');
78-
expect(rpcCookies[2].sameSite).to.equal(RpcHttpCookie.SameSite.Strict);
77+
expect(rpcCookies[2].sameSite).to.equal('strict');
7978

8079
expect(rpcCookies[3].name).to.equal('default-cookie');
81-
expect(rpcCookies[3].sameSite).to.equal(RpcHttpCookie.SameSite.None);
80+
expect(rpcCookies[3].sameSite).to.equal('none');
8281
});
8382

8483
it('throws on invalid input', () => {

test/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44
import * as globby from 'globby';
55
import * as Mocha from 'mocha';
66
import * as path from 'path';
7-
import { setupTestCoreApi } from './setupTestCoreApi';
87

98
export async function run(): Promise<void> {
109
try {
11-
setupTestCoreApi();
12-
1310
const options: Mocha.MochaOptions = {
1411
color: true,
1512
reporter: 'mocha-multi-reporters',

0 commit comments

Comments
 (0)