add cloudevents-sdk/v03 types (#38067)

* add cloudevents-sdk/v03 types

ignore no-single-declare-module because cloudevents-sdk/v03 is the documented way to import the cloudevent builder

add cloudevents-sdk header

module warning ignore

* move CloudEvent interface into module, use named export for event()

* use default export with event() as a prop to fix 'no default export' TS warning

* allowSyntheticImports for cloudevents-sdk
This commit is contained in:
andy cunningham 2020-03-09 12:36:57 -07:00 committed by GitHub
parent 0db40cd545
commit 540cea2e2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 0 deletions

View File

@ -0,0 +1,14 @@
import Cloudevent from 'cloudevents-sdk/v03';
function testEventBuilder() {
const event = Cloudevent.event()
.dataContentType('application/json')
.id('43')
.time(new Date())
.type('pull_request')
.source('github')
.data({ action: 'created' })
.format();
const { id, datacontenttype, time, type, source, data } = event;
}

36
types/cloudevents-sdk/index.d.ts vendored Normal file
View File

@ -0,0 +1,36 @@
// Type definitions for cloudevents-sdk 0.3
// Project: https://github.com/cloudevents/sdk-javascript (Does not have to be to GitHub, but prefer linking to a source code repository rather than to a project website.)
// Definitions by: Andy Cunningham <https://github.com/andycmaj>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// tslint:disable-next-line no-declare-current-package no-single-declare-module cloudevents-sdk/v03 is the documented way to import the cloudevent builder
declare module 'cloudevents-sdk/v03' {
// v03 CloudEvent payload
// https://github.com/cloudevents/sdk-javascript/blob/master/lib/specs/spec_0_3.js#L11
interface CloudEvent {
id: string;
datacontenttype: 'application/json';
time: string;
type: string;
source: string;
data: {};
}
// Cloudevent fluent builder
// https://github.com/cloudevents/sdk-javascript/blob/master/lib/cloudevent.js
interface CloudEventBuilder {
type: (type: string) => CloudEventBuilder;
dataContentType: (contentType: string) => CloudEventBuilder;
id: (id: string) => CloudEventBuilder;
time: (timestamp: Date) => CloudEventBuilder;
source: (source: string) => CloudEventBuilder;
data: (data: any) => CloudEventBuilder;
format: () => CloudEvent;
toString: () => string;
}
// Cloudevent.event() static constructor
// https://github.com/cloudevents/sdk-javascript#an-easy-way-to-create-events
function event(): CloudEventBuilder;
}

View File

@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"allowSyntheticDefaultImports": true
},
"files": [
"index.d.ts",
"cloudevents-sdk-tests.ts"
]
}

View File

@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}