Skip to content

Commit 82b3d45

Browse files
authored
Refactor to isolate public interfaces (Azure#136)
* moved public interface components to separate file * trying to incude interfaces types * ! gitignores * excluding types except for interfaces * add bindingData * remove reference to IDict * update .gitignore comment
1 parent 53d79a0 commit 82b3d45

11 files changed

+149
-55
lines changed

.gitignore

+8-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,11 @@ test/end-to-end/testFunctionApp/bin
4646
test/end-to-end/testFunctionApp/obj
4747
test/end-to-end/Azure.Functions.NodejsWorker.E2E/.vs
4848
test/end-to-end/Azure.Functions.NodejsWorker.E2E/Azure.Functions.NodejsWorker.E2E/bin
49-
test/end-to-end/Azure.Functions.NodejsWorker.E2E/Azure.Functions.NodejsWorker.E2E/obj
49+
test/end-to-end/Azure.Functions.NodejsWorker.E2E/Azure.Functions.NodejsWorker.E2E/obj
50+
51+
# Commit external interfaces
52+
types/*
53+
54+
!types/public
55+
types/public/*
56+
!types/public/Interfaces.d.ts

src/Context.ts

+2-34
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,7 @@ import { AzureFunctionsRpcMessages as rpc } from '../azure-functions-language-wo
44
import { Request, HttpRequest } from './http/Request';
55
import { Response } from './http/Response';
66
import LogLevel = rpc.RpcLog.Level;
7-
8-
export interface IContext {
9-
invocationId: string;
10-
executionContext: IExecutionContext;
11-
bindings: IDict<any>;
12-
bindingData: IDict<any>;
13-
bindingDefinitions: IDict<any>[];
14-
log: ILogger;
15-
req?: Request;
16-
res?: Response;
17-
done: IDoneCallback;
18-
};
7+
import { IContext, IExecutionContext, ILogger, IDoneCallback } from './public/Interfaces'
198

209
export function CreateContextAndInputs(info: FunctionInfo, request: rpc.IInvocationRequest, logCallback: ILogCallback, callback: IResultCallback) {
2110
let context = new Context(info, request, logCallback, callback);
@@ -115,17 +104,6 @@ export interface IInvocationResult {
115104
bindings: IDict<any>;
116105
}
117106

118-
export interface ILog {
119-
(...args: any[]): void;
120-
}
121-
122-
export interface ILogger extends ILog {
123-
error: ILog;
124-
warn: ILog;
125-
info: ILog;
126-
verbose: ILog;
127-
}
128-
129107
export interface ILogCallback {
130108
(level: LogLevel, ...args: any[]): void;
131109
}
@@ -134,16 +112,6 @@ export interface IResultCallback {
134112
(err?: any, result?: IInvocationResult): void;
135113
}
136114

137-
export interface IDoneCallback {
138-
(err?: any, result?: any): void;
139-
}
140-
141-
export interface IExecutionContext {
142-
invocationId: string;
143-
functionName: string;
144-
functionDirectory: string;
145-
}
146-
147115
export interface IDict<T> {
148116
[key: string]: T
149-
}
117+
}

src/Converters.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AzureFunctionsRpcMessages as rpc } from '../azure-functions-language-worker-protobuf/src/rpc';
22
import { FunctionInfo } from './FunctionInfo';
33
import { HttpRequest } from './http/Request';
4-
import { IDict } from './Context';
4+
import { IDict } from '../src/Context';
55
export function fromRpcHttp(rpcHttp: rpc.IRpcHttp) {
66
let httpContext: HttpRequest = {
77
method: <string>rpcHttp.method,

src/WorkerChannel.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Duplex } from 'stream';
21
import { format, isFunction } from 'util';
32

43
import { AzureFunctionsRpcMessages as rpc } from '../azure-functions-language-worker-protobuf/src/rpc';

src/http/Request.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
import { IRequest } from '../public/Interfaces';
34

45
export class HttpRequest {
56
method: string = "";
@@ -13,13 +14,13 @@ export class HttpRequest {
1314
[key:string]: any;
1415
}
1516

16-
export class Request extends HttpRequest {
17+
export class Request extends HttpRequest implements IRequest {
1718
constructor(httpInput: HttpRequest) {
1819
super();
1920
Object.assign(this, httpInput);
2021
}
2122

22-
get(field: string) {
23+
get(field: string): string | undefined {
2324
return this.headers && this.headers[field.toLowerCase()];
2425
}
2526
}

src/http/Response.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Copyright (c) .NET Foundation. All rights thiserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
import { IResponse } from '../public/Interfaces';
34

4-
export class Response {
5+
export class Response implements IResponse {
56
statusCode?: string | number;
67
headers: {[key:string]: any} = {};
78
body?: any;
@@ -23,41 +24,43 @@ export class Response {
2324
return this;
2425
}
2526

26-
setHeader(field: string, val: any) {
27+
setHeader(field: string, val: any): IResponse {
2728
this.headers[field.toLowerCase()] = val;
2829
return this;
2930
}
3031

31-
getHeader(field: string) {
32+
getHeader(field: string): IResponse {
3233
return this.headers[field.toLowerCase()];
3334
}
3435

35-
removeHeader(field: string){
36+
removeHeader(field: string) {
3637
delete this.headers[field.toLowerCase()];
3738
return this;
3839
}
3940

40-
status(statusCode: string | number){
41+
status(statusCode: string | number): IResponse {
4142
this.statusCode = statusCode;
4243
return this;
4344
}
4445

45-
sendStatus(statusCode: string | number){
46-
return this.status(statusCode)
47-
.end();
46+
sendStatus(statusCode: string | number) {
47+
this.status(statusCode);
48+
return this.end();
4849
}
4950

5051
type(type){
5152
return this.set('content-type', type);
5253
}
5354

5455
json(body){
55-
return this.type('application/json')
56-
.send(body);
56+
this.type('application/json');
57+
this.send(body);
58+
return;
5759
}
5860

5961
send = this.end;
60-
header = this.set = this.setHeader;
62+
header = this.setHeader;
63+
set = this.setHeader;
6164
get = this.getHeader;
6265

6366
private setContentType() {

src/public/Interfaces.ts

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
export interface IContext {
2+
invocationId: string;
3+
executionContext: IExecutionContext;
4+
bindings: { [key: string]: any };
5+
bindingData: { [key: string]: any };
6+
bindingDefinitions: { [key: string]: any };
7+
log: ILogger;
8+
req?: IRequest;
9+
res?: IResponse;
10+
done: IDoneCallback;
11+
};
12+
13+
export interface IExecutionContext {
14+
invocationId: string;
15+
functionName: string;
16+
functionDirectory: string;
17+
}
18+
19+
export interface ILog {
20+
(...args: any[]): void;
21+
}
22+
23+
export interface ILogger extends ILog {
24+
error: ILog;
25+
warn: ILog;
26+
info: ILog;
27+
verbose: ILog;
28+
}
29+
30+
export interface IRequest {
31+
method: string;
32+
url: string;
33+
originalUrl: string;
34+
headers?: {[key:string]: string};
35+
query?: {[key:string]: string};
36+
params?: {[key:string]: string};
37+
body?: any;
38+
rawbody?: any;
39+
get(field: string): string | undefined;
40+
}
41+
42+
export interface IResponse {
43+
statusCode?: string | number;
44+
headers: {[key:string]: any};
45+
body?: any;
46+
get(field: string): any;
47+
set(field: string, val: any): IResponse;
48+
header(field: string, val: any): IResponse;
49+
status(statusCode: string | number): IResponse;
50+
}
51+
52+
export interface IDoneCallback {
53+
(err?: any, result?: any): void;
54+
}

test/ContextTests.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { IContext, CreateContextAndInputs, ILogCallback, IResultCallback } from '../src/Context';
1+
import { CreateContextAndInputs, ILogCallback, IResultCallback } from '../src/Context';
2+
import { IContext } from "../src/public/Interfaces";
23
import { FunctionInfo } from '../src/FunctionInfo';
34
import { AzureFunctionsRpcMessages as rpc } from '../azure-functions-language-worker-protobuf/src/rpc';
4-
import { expect } from 'chai';
55
import * as sinon from 'sinon';
66
import 'mocha';
77

test/FunctionLoaderTests.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { WorkerChannel } from '../src/WorkerChannel';
22
import { FunctionLoader } from '../src/FunctionLoader';
33
import { expect } from 'chai';
4-
import * as sinon from 'sinon';
54
import { AzureFunctionsRpcMessages as rpc } from '../azure-functions-language-worker-protobuf/src/rpc';
65
import 'mocha';
76
import mock = require('mock-require');
8-
import { isObject } from 'util';
97

108
describe('FunctionLoader', () => {
119
var channel: WorkerChannel;

tsconfig.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"noImplicitAny": false,
66
"strict": true,
77
"outDir": "dist/src",
8-
"sourceMap": true
8+
"sourceMap": true,
9+
"declaration": true,
10+
"declarationDir": "types"
911
},
1012
"include": [
1113
"src/**/*"

types/public/Interfaces.d.ts

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
export interface IContext {
2+
invocationId: string;
3+
executionContext: IExecutionContext;
4+
bindings: {
5+
[key: string]: any;
6+
};
7+
bindingData: {
8+
[key: string]: any;
9+
};
10+
bindingDefinitions: {
11+
[key: string]: any;
12+
};
13+
log: ILogger;
14+
req?: IRequest;
15+
res?: IResponse;
16+
done: IDoneCallback;
17+
}
18+
export interface IExecutionContext {
19+
invocationId: string;
20+
functionName: string;
21+
functionDirectory: string;
22+
}
23+
export interface ILog {
24+
(...args: any[]): void;
25+
}
26+
export interface ILogger extends ILog {
27+
error: ILog;
28+
warn: ILog;
29+
info: ILog;
30+
verbose: ILog;
31+
}
32+
export interface IRequest {
33+
method: string;
34+
url: string;
35+
originalUrl: string;
36+
headers?: {
37+
[key: string]: string;
38+
};
39+
query?: {
40+
[key: string]: string;
41+
};
42+
params?: {
43+
[key: string]: string;
44+
};
45+
body?: any;
46+
rawbody?: any;
47+
get(field: string): string | undefined;
48+
}
49+
export interface IResponse {
50+
statusCode?: string | number;
51+
headers: {
52+
[key: string]: any;
53+
};
54+
body?: any;
55+
get(field: string): any;
56+
set(field: string, val: any): IResponse;
57+
header(field: string, val: any): IResponse;
58+
status(statusCode: string | number): IResponse;
59+
}
60+
export interface IDoneCallback {
61+
(err?: any, result?: any): void;
62+
}

0 commit comments

Comments
 (0)