简体   繁体   English

如何在 typescript 中导出函数

[英]How to export functions in typescript

Total Typescript newbie.总Typescript新手。 I'm working on a cdk project.我正在做一个 cdk 项目。 I have a class (stack) that is getting massive.我有一个越来越大的 class(堆栈)。 So I want to offload a bunch of methods into other files to keep it manageable.所以我想将一堆方法卸载到其他文件中以使其易于管理。

This works:这有效:

import * as cdk from '@aws-cdk/core';
import * as lambda        from '@aws-cdk/aws-lambda';                // need for Runtime
import * as lambda_nodejs from '@aws-cdk/aws-lambda-nodejs';

const TAG = "sn-v1-";

export class SnProdStack extends cdk.Stack {
    constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
      super(scope, id, props);

      // Lambda: Get stuff from Sigfox API
      // ---------------------------------
      
      const testLambda = new lambda_nodejs.NodejsFunction(this, 'sigfox-get', {
        runtime: lambda.Runtime.NODEJS_12_X,
        entry: 'lambda/sn-prod/sigfox-get.js',
        handler: 'handler',
        functionName: TAG + 'sigfox-get',
        description: 'Get all devices from Sigfox API.',
        memorySize: 256,
        timeout: cdk.Duration.seconds(360)
      });
    }
}

But I want to do something like this:但我想做这样的事情:

// class file
import * as cdk from '@aws-cdk/core';
import * as lambda        from '@aws-cdk/aws-lambda';                // need for Runtime
import * as lambda_nodejs from '@aws-cdk/aws-lambda-nodejs';

import { genSigfoxGetLambda } from './sn-prod/api-gw';

export class SnProdStack extends cdk.Stack {
    constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
      super(scope, id, props);      
      const testLambda = this.genSigfoxGetLambda;
    }
}



// method file
import { SnProdStack } from '../sn-prod-stack';

import * as cdk from '@aws-cdk/core';
import * as lambda        from '@aws-cdk/aws-lambda';                // need for Runtime
import * as lambda_nodejs from '@aws-cdk/aws-lambda-nodejs';

const TAG = "sn-v1-";

export function genSigfoxGetLambda(this: SnProdStack) {
    const testLambda = new lambda_nodejs.NodejsFunction(this, 'sigfox-get', {
        runtime: lambda.Runtime.NODEJS_12_X,
        entry: 'lambda/sn-prod/sigfox-get.js',
        handler: 'handler',
        functionName: TAG + 'sigfox-get',
        description: 'Get all devices from Sigfox API.',
        memorySize: 256,
        timeout: cdk.Duration.seconds(360)
      });
      return testLambda;
}

This is a tiny minimal reproducible example.这是一个很小的最小可重现示例。 The whole code is enormous, with 10 or 20 of each thing.整个代码非常庞大,每件事都有 10 或 20 个。 My plan is to use factory methods for each type of tying I need to make and pass in an options object.我的计划是对我需要制作的每种类型的绑定使用工厂方法,并传入一个选项 object。 Right now, the problem is that the code I've written does nothing at all.现在,问题是我写的代码什么都不做。

To export a function, we need to add the export directive and to make it available to other files outside of its current folder.要导出 function,我们需要添加export指令并使其可用于当前文件夹之外的其他文件。

You are already using the export keyword, so now all you need to do is place an index.ts file in every folder.您已经在使用export关键字,所以现在您需要做的就是在每个文件夹中放置一个index.ts文件。

For example, if we have this directory structure...例如,如果我们有这个目录结构......

.
├── main.ts
└── module
    ├── a.ts
    └── index.ts

...and we wish to export a function from a.ts and use it in main.ts , then the function must use the export keyword and index.ts must export it from module/ . ...并且我们希望从a.ts并在main.ts中使用它,然后 function 必须使用export关键字,并且index.ts必须从module/导出它。

// a.ts
export const foo = () => { return 'bar'; };
// index.ts
export * from './a';

Then, we may import the function.然后,我们可以导入 function。

// main.ts
import { foo } from './module';

Try something like this from your main stack从你的主堆栈中尝试这样的事情

import cdk = require("@aws-cdk/core");
import { MyFirstStack } from "../lib/my-first-stack";
import { StepFunctionStack } from "../lib/step-function-stack";

const app = new cdk.App();
const myFirstStack = new MyFirstStack(app, "stackName");
const stepFunctionStack = new StepFunctionStack(app, "stepFunctionStackName");

// If you need to add a dependency on one of your stacks
myFirstStack.addDependency(stepFunctionStack);

Then you can have the other stacks in the location imported from above, in my example its back 1 folder and in a lib folder..然后您可以在从上面导入的位置中的其他堆栈,在我的示例中,它的后 1 文件夹和 lib 文件夹中..

Here is something what one of them stacks would look like这是其中一个堆栈的样子

import { Construct, Stack, StackProps } from "@aws-cdk/core";

export class MyFirstStack extends Stack {
  constructor(scope: App, id: string, props?: StackProps) {
    const properties: StackProps = {
      env: {
        region: "us-east-1",
        account: <account>,
      },
    };

    super(scope, id, properties);

    const lambda = create_lambda()

    create_lambda(){
    // code to create lambda etc. 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM