Skip to content

Commit ad4ab12

Browse files
committed
add success method to progress class
1 parent 8bda729 commit ad4ab12

File tree

2 files changed

+52
-121
lines changed

2 files changed

+52
-121
lines changed

src/proxy.ts

Lines changed: 28 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,19 @@
1-
import { AWSError, CredentialProviderChain, Request, Service } from 'aws-sdk';
21
import { ConfigurationOptions } from 'aws-sdk/lib/config';
3-
import { Credentials, CredentialsOptions } from 'aws-sdk/lib/credentials';
4-
import { ServiceConfigurationOptions } from 'aws-sdk/lib/service';
2+
import { CredentialsOptions } from 'aws-sdk/lib/credentials';
53
import * as Aws from 'aws-sdk/clients/all';
64
import { NextToken } from 'aws-sdk/clients/cloudformation';
75
import { allArgsConstructor, builder, IBuilder} from 'tombok';
86

97
import {
108
BaseResourceHandlerRequest,
119
BaseResourceModel,
12-
Callable,
1310
HandlerErrorCode,
14-
Newable,
1511
OperationStatus,
1612
} from './interface';
1713

1814

1915
type ClientMap = typeof Aws;
2016
type Client = InstanceType<ClientMap[keyof ClientMap]>;
21-
type ClientType<T = ClientMap> = T[keyof T] extends Service ? never : T[keyof T];
22-
23-
// type Async<T> = T extends AsyncGenerator<infer R> ? AsyncGenerator<R> : T extends Generator<infer R> ? AsyncGenerator<R> : T extends Promise<infer R> ? Promise<R> : Promise<T>;
24-
25-
// type ProxyModule<M> = {
26-
// [K in keyof M]: M[K] extends (...args: infer A) => infer R ? (...args: A) => Async<R> : never;
27-
// };
28-
29-
// type Callback<D> = (err: AWSError | undefined, data: D) => void;
30-
31-
// interface AWSRequestMethod<P, D> {
32-
// (params: P, callback?: Callback<D>): Request<D, AWSError>;
33-
// (callback?: Callback<D>): Request<D, AWSError>;
34-
// }
35-
36-
// export type CapturedAWSClient<C extends AWSClient> = {
37-
// [K in keyof C]: C[K] extends AWSRequestMethod<infer P, infer D>
38-
// ? AWSRequestMethod<P, D>
39-
// : C[K];
40-
// };
41-
42-
// export type CapturedAWS<T = ClientMap> = {
43-
// [K in keyof T]: T[K] extends AWSClient ? CapturedAWSClient<T[K]> : T[K];
44-
// };
45-
46-
// export function captureAWSClient<C extends AWSClient>(
47-
// client: C
48-
// ): CapturedAWSClient<C>;
49-
// export function captureAWS(awssdk: ClientMap): CapturedAWS;
50-
51-
// type Clients = { [K in keyof AwsClientMap]?: AwsClientMap[K] extends Service ? never : AwsClientMap[K] };
52-
53-
class SessionCredentialsProvider {
54-
55-
private awsSessionCredentials: Credentials;
56-
57-
public get(): Credentials {
58-
return this.awsSessionCredentials;
59-
}
60-
61-
public setCredentials(credentials: CredentialsOptions): void {
62-
this.awsSessionCredentials = new Credentials(credentials);
63-
}
64-
}
6517

6618
export class SessionProxy {
6719

@@ -75,37 +27,7 @@ export class SessionProxy {
7527
...this.options,
7628
...options,
7729
});
78-
return service; //this.promisifyReturn(service);
79-
}
80-
81-
// private createService<T extends ClientMap, K extends keyof T>(client: Newable<T[K]>, options: ServiceConfigurationOptions): InstanceType<ClientType> {
82-
// // const clients: { [K in keyof ClientMap]?: ClientMap[K] } = Aws;
83-
// // const name: T;
84-
85-
// return new client();
86-
// }
87-
88-
// Wraps an Aws endpoint instance so that you don’t always have to chain `.promise()` onto every function
89-
public promisifyReturn(obj: any): ProxyConstructor {
90-
return new Proxy(obj, {
91-
get(target, propertyKey) {
92-
const property = target[propertyKey];
93-
94-
if (typeof property === "function") {
95-
return function (...args: any[]) {
96-
const result = property.apply(this, args);
97-
98-
if (result instanceof Request) {
99-
return result.promise();
100-
} else {
101-
return result;
102-
}
103-
}
104-
} else {
105-
return property;
106-
}
107-
},
108-
});
30+
return service;
10931
}
11032

11133
public static getSession(credentials?: CredentialsOptions, region?: string): SessionProxy | null {
@@ -152,7 +74,7 @@ export class ProgressEvent<R extends BaseResourceModel = BaseResourceModel, T =
15274
* A callback will be scheduled with an initial delay of no less than the number
15375
* of seconds specified in the progress event.
15476
*/
155-
public callbackDelaySeconds: number;
77+
public callbackDelaySeconds: number = 0;
15678

15779
/**
15880
* The output resource instance populated by a READ for synchronous results and
@@ -176,7 +98,6 @@ export class ProgressEvent<R extends BaseResourceModel = BaseResourceModel, T =
17698

17799
public serialize(
178100
toTesponse: boolean = false, bearerToken?: string
179-
// ): Record<string, any> {
180101
): Map<string, any> {
181102
// To match Java serialization, which drops `null` values, and the
182103
// contract tests currently expect this also.
@@ -226,14 +147,33 @@ export class ProgressEvent<R extends BaseResourceModel = BaseResourceModel, T =
226147
/**
227148
* Convenience method for constructing IN_PROGRESS response
228149
*/
229-
public static progress(model: any, cxt: any): ProgressEvent {
230-
const event = ProgressEvent.builder()
231-
.callbackContext(cxt)
232-
.resourceModel(model)
233-
.status(OperationStatus.InProgress)
234-
.build();
150+
public static progress(model?: any, ctx?: any): ProgressEvent {
151+
const progress = ProgressEvent.builder()
152+
.status(OperationStatus.InProgress);
153+
if (ctx) {
154+
progress.callbackContext(ctx);
155+
}
156+
if (model) {
157+
progress.resourceModel(model);
158+
}
159+
const event = progress.build();
160+
return event;
161+
}
162+
163+
/**
164+
* Convenience method for constructing a SUCCESS response
165+
*/
166+
public static success(model?: any, ctx?: any): ProgressEvent {
167+
const event = ProgressEvent.progress(model, ctx);
168+
event.status = OperationStatus.Success;
235169
return event;
236170
}
171+
172+
public toObject(): Object {
173+
// @ts-ignore
174+
const obj: Object = Object.fromEntries(this.serialize().entries());
175+
return obj;
176+
}
237177
}
238178

239179
/**

tests/lib/proxy.test.ts

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,12 @@ describe('when getting session proxy', () => {
1414

1515
const BEARER_TOKEN: string = 'f3390613-b2b5-4c31-a4c6-66813dff96a6';
1616

17-
@builder
18-
@allArgsConstructor
19-
class ResourceModel extends Map<string, any> implements BaseResourceModel {
20-
21-
public static typeName: string = 'Test::Resource::Model';
22-
17+
class ResourceModel extends BaseResourceModel {
18+
19+
public static readonly TYPE_NAME: string = 'Test::Resource::Model';
20+
2321
public somekey: Optional<string>;
2422
public someotherkey: Optional<string>;
25-
26-
constructor(...args: any[]) {super()}
27-
public static builder(template?: Partial<ResourceModel>): IBuilder<ResourceModel> {return null}
28-
29-
serialize(): Map<string, any> {
30-
const data: Map<string, any> = new Map<string, any>(Object.entries(JSON.parse(JSON.stringify(this))));
31-
data.forEach((value: any, key: string) => {
32-
if (value == null) {
33-
data.delete(key);
34-
}
35-
});
36-
return data;
37-
}
38-
deserialize(): ResourceModel {return <ResourceModel>undefined}
39-
toObject(): Object {
40-
// @ts-ignore
41-
const obj = Object.fromEntries(this.serialize().entries());
42-
return obj;
43-
}
4423
}
4524

4625
test('get session returns proxy', () => {
@@ -69,14 +48,14 @@ describe('when getting session proxy', () => {
6948
status: OperationStatus.Failed,
7049
errorCode: errorCode,
7150
message,
72-
// callbackDelaySeconds: 0,
51+
callbackDelaySeconds: 0,
7352
})));
7453
});
7554

7655
test('progress event serialize to response with context', () => {
7756
const message: string = 'message of event with context';
7857
const event = ProgressEvent.builder()
79-
.callbackContext({ "a": "b" })
58+
.callbackContext({ a: 'b' })
8059
.message(message)
8160
.status(OperationStatus.Success)
8261
.build();
@@ -91,7 +70,8 @@ describe('when getting session proxy', () => {
9170
test('progress event serialize to response with model', () => {
9271
const message = 'message of event with model';
9372
const model = new ResourceModel(new Map(Object.entries({
94-
"somekey": "a", "someotherkey": "b"
73+
somekey: 'a',
74+
someotherkey: 'b',
9575
})));
9676
const event = new ProgressEvent(new Map(Object.entries({
9777
status: OperationStatus.Success,
@@ -103,17 +83,22 @@ describe('when getting session proxy', () => {
10383
operationStatus: OperationStatus.Success,
10484
message,
10585
bearerToken: BEARER_TOKEN,
106-
resourceModel: {"somekey": "a", "someotherkey": "b"},
86+
resourceModel: {
87+
somekey: 'a',
88+
someotherkey: 'b',
89+
},
10790
})));
10891
});
10992

11093
test('progress event serialize to response with models', () => {
11194
const message = 'message of event with models';
11295
const models = [new ResourceModel(new Map(Object.entries({
113-
"somekey": "a", "someotherkey": "b"
96+
somekey: 'a',
97+
someotherkey: 'b',
11498
}))),
11599
new ResourceModel(new Map(Object.entries({
116-
"somekey": "c", "someotherkey": "d"
100+
somekey: 'c',
101+
someotherkey: 'd',
117102
})))];
118103
const event = new ProgressEvent(new Map(Object.entries({
119104
status: OperationStatus.Success,
@@ -126,8 +111,14 @@ describe('when getting session proxy', () => {
126111
message,
127112
bearerToken: BEARER_TOKEN,
128113
resourceModels: [
129-
{"somekey": "a", "someotherkey": "b"},
130-
{"somekey": "c", "someotherkey": "d"},
114+
{
115+
somekey: 'a',
116+
someotherkey: 'b',
117+
},
118+
{
119+
somekey: 'c',
120+
someotherkey: 'd',
121+
},
131122
],
132123
})));
133124
});

0 commit comments

Comments
 (0)