Skip to content

Commit

Permalink
[serverless] fix hooks and cloudformation.resources definitions (Defi…
Browse files Browse the repository at this point in the history
…nitelyTyped#38315)

* Update serverless types

This fixes a couple of inconsistencies with the type declarations:
* `Plugin.hooks` is a dictionary of functions that return a promise, not a dictionary of promises
* `serverless.service.provider.compiledCloudFormationTemplate.Resources` is an object rather than an array

* make Plugin an interface instead of an abstract class

* add provider.naming

* add plugin constructor typedef as PluginStatic
  • Loading branch information
timtrinidad authored and uniqueiniquity committed Oct 1, 2019
1 parent d286aff commit 20a5f8e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
9 changes: 5 additions & 4 deletions types/serverless/classes/Plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ declare namespace Plugin {
};
};
}

interface PluginStatic {
new (serverless: Serverless, options: Serverless.Options): Plugin;
}
}

declare abstract class Plugin {
interface Plugin {
hooks: Plugin.Hooks;

commands?: Plugin.Commands;

constructor(serverless: Serverless, options: Serverless.Options);
}

export = Plugin;
2 changes: 1 addition & 1 deletion types/serverless/classes/PluginManager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ declare class PluginManager {
setCliOptions(options: Serverless.Options): void;
setCliCommands(commands: {}): void;

addPlugin(plugin: typeof Plugin): void;
addPlugin(plugin: Plugin.PluginStatic): void;
loadAllPlugins(servicePlugins: {}): void;
loadPlugins(plugins: {}): void;
loadCorePlugins(): void;
Expand Down
4 changes: 3 additions & 1 deletion types/serverless/classes/Service.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ declare class Service {

provider: {
compiledCloudFormationTemplate: {
Resources: any[];
Resources: {
[key: string]: any;
};
Outputs?: {
[key: string]: any;
};
Expand Down
1 change: 1 addition & 0 deletions types/serverless/plugins/aws/provider/awsProvider.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Serverless = require("../../../index");
declare class Aws {
constructor(serverless: Serverless, options: Serverless.Options)

naming: { [key: string]: () => string };
getProviderName(): string;
getRegion(): string;
getServerlessDeploymentBucketName(): string;
Expand Down
15 changes: 13 additions & 2 deletions types/serverless/serverless-tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Serverless from 'serverless';
import Plugin from 'serverless/classes/Plugin';
import PluginManager from 'serverless/classes/PluginManager';

const options: Serverless.Options = {
noDeploy: false,
Expand All @@ -9,7 +10,7 @@ const options: Serverless.Options = {

const serverless = new Serverless();

class CustomPlugin extends Plugin {
class CustomPlugin implements Plugin {
commands = {
command: {
usage: 'description',
Expand All @@ -29,9 +30,19 @@ class CustomPlugin extends Plugin {
hooks: Plugin.Hooks;

constructor(serverless: Serverless, options: Serverless.Options) {
super(serverless, options);
this.hooks = {
'command:start': () => {},
};
}
}

// Test a plugin with missing 'hooks' property
class BadPlugin implements Plugin { // $ExpectError
hoooks: Plugin.Hooks; // emulate a bad 'hooks' definition with a typo
constructor(badArg: number) {}
}

const manager = new PluginManager(serverless);
manager.addPlugin(CustomPlugin);
// Test adding a plugin with an incorrect constructor
manager.addPlugin(BadPlugin); // $ExpectError

0 comments on commit 20a5f8e

Please sign in to comment.