使用 node js + typescript 导入条带 [英] import stripe using node js + typescript

查看:30
本文介绍了使用 node js + typescript 导入条带的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将条带导入到我的应用程序中

I need to import stripe in to my application

首先我安装了stripe npm包

First i installed stripe npm package

npm install stripe --save

Stripe 文档说在连接 api 之前应该设置密钥.

Stripe documentation says that secret key should be set before connect the api.

在 Node 中它喜欢这个

In Node it likes this

var stripe = require('stripe')(' your stripe API key ');

我需要把它转换成打字稿

I need to convert it to typescript

我尝试了以下方法.但它对我不起作用

I tried following way . but it didnt work for me

import * as stripe from 'stripe';
stripe('sk_test_...')

如果有人能帮我解决这个问题,这将对我毫不拖延地继续我的项目有很大帮助.

if some can help me to solve this problem it will great help to me to continue my project without delay.

谢谢

推荐答案

更新: 下面的解决方案已经过时,对于那些使用 stripe@8.0.1 或更高版本的解决方案答案来自David Dehghan 应该被使用.

Update: The solution below is outdated and for those using stripe@8.0.1 or greater the answer from David Dehghan should be used.

正如 britzkopf 所说,stripe 尚未提供自己的定义(可能永远不会),但您可以使用 @types/stripe 中的类型定义.

As britzkopf said, stripe don't provide their own definitions yet (and probably never will), but you can use the type definitions from @types/stripe.

npm install stripe @types/stripe

然后导入并构造Stripe类,如下所示.

And then import and construct the Stripe class as follows.

import * as Stripe from 'stripe';
const stripe = new Stripe('xxx_xxx_xxx');

如果您出于某种原因想要更细粒度的导入,您可以改用这种(有点笨拙的)方法.

If you want finer grained imports for some reason you can use this (somewhat hacky) approach instead.

import { resources } from 'stripe';
const stripeData = require('stripe')('xxx_xxx_xxx');
const customers = new resources.Customers(stripeData, null);

这篇关于使用 node js + typescript 导入条带的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆