diff --git a/types/custom-functions-runtime/custom-functions-runtime-tests.ts b/types/custom-functions-runtime/custom-functions-runtime-tests.ts new file mode 100644 index 0000000000..335e5a1f95 --- /dev/null +++ b/types/custom-functions-runtime/custom-functions-runtime-tests.ts @@ -0,0 +1,33 @@ +/* Note that this is a sample function for the purpose of running the below CustomFunctionsMappings test */ +function ADD10(n: number) { + return n + 10; +} + +CustomFunctionMappings = { + addTen: ADD10 +}; + +async function getStockValues(ticker: string, handler: CustomFunctions.StreamingHandler) { + const dollars = await (await fetch(`myService.com/prices/${ticker}`)).json(); + handler.setResult(dollars); +} + +async function getStockValuesOneTime(ticker: string, handler: CustomFunctions.CancelableHandler) { + let shouldStop = false; + handler.onCanceled = () => shouldStop = true; + await pause(1000); + + if (shouldStop) { + return null; + } + + const dollars = await (await fetch(`myService.com/prices/${ticker}`)).json(); + return dollars; +} + +async function getStockValuesNowWithNoCancelling(ticker: string) { + const dollars = await (await fetch(`myService.com/prices/${ticker}`)).json(); + return dollars; +} + +declare function pause(ms: number): Promise; diff --git a/types/custom-functions-runtime/index.d.ts b/types/custom-functions-runtime/index.d.ts new file mode 100644 index 0000000000..e233a869fd --- /dev/null +++ b/types/custom-functions-runtime/index.d.ts @@ -0,0 +1,30 @@ +// Type definitions for Custom Functions 1.4 +// Project: http://dev.office.com +// Definitions by: OfficeDev , Michael Zlatkovsky , Michelle Scharlock +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.4 + +/* +office-js +Copyright (c) Microsoft Corporation +*/ + +declare let CustomFunctionMappings: { [key: string]: Function }; + +declare namespace CustomFunctions { + interface StreamingHandler extends CancelableHandler { + /* + * Sets the returned result for a streaming custom function. + * @beta + */ + setResult: (value: T) => void; + } + + interface CancelableHandler { + /* + * Handles what should occur when a custom function is canceled. + * @beta + */ + onCanceled: () => void; + } +} diff --git a/types/custom-functions-runtime/tsconfig.json b/types/custom-functions-runtime/tsconfig.json new file mode 100644 index 0000000000..3c8cbbaf27 --- /dev/null +++ b/types/custom-functions-runtime/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "target": "es6" + }, + "files": [ + "index.d.ts", + "custom-functions-runtime-tests.ts" + ] +} diff --git a/types/custom-functions-runtime/tslint.json b/types/custom-functions-runtime/tslint.json new file mode 100644 index 0000000000..a62d0d4e68 --- /dev/null +++ b/types/custom-functions-runtime/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "ban-types": false + } +}