Skip to content

Commit e8b2276

Browse files
jeskewsrchase
authored andcommitted
Create build types package (#65)
* Move middlewarestack interface to @aws/types * Add configuration types * Create a package to house types used at build time but not at runtime and relocate model parser types there
1 parent a84d69d commit e8b2276

File tree

4 files changed

+155
-88
lines changed

4 files changed

+155
-88
lines changed

packages/middleware-stack/src/index.ts

Lines changed: 5 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {
22
Handler,
3+
HandlerOptions,
34
Middleware,
5+
MiddlewareStack as IMiddlewareStack,
46
HandlerExecutionContext,
57
} from '@aws/types';
68

@@ -15,69 +17,12 @@ interface HandlerListEntry<
1517
tags?: Set<string>;
1618
}
1719

18-
export interface HandlerOptions {
19-
/**
20-
* Handlers are ordered using a "step" that describes the stage of command
21-
* execution at which the handler will be executed. The available steps are:
22-
*
23-
* - initialize: The input is being prepared and validated. Examples of
24-
* typical initialization tasks include injecting default options and
25-
* parameter validation.
26-
* - build: The input is being serialized into an HTTP request. The input
27-
* should not be altered in middleware occupying this step, as it may
28-
* have already been serialized into a request. Examples of typical
29-
* build tasks include request construction and injecting HTTP headers.
30-
* - finalize: The request is being prepared to be sent over the wire. The
31-
* request in this stage should already be semantically complete and
32-
* should therefore only be altered as match the recipient's
33-
* expectations. Examples of typical finalization tasks include request
34-
* signing and injecting hop-by-hop headers.
35-
*
36-
* Unlike initialization and build handlers, which are executed once
37-
* per operation execution, finalization handlers will be executed for
38-
* each HTTP request sent.
39-
*
40-
* @default 'build'
41-
*/
42-
step?: Step;
43-
44-
/**
45-
* A number that specifies how early in a given step of the middleware stack
46-
* a handler should be executed. Higher numeric priorities will be executed
47-
* earlier.
48-
*
49-
* @default 0
50-
*/
51-
priority?: number;
52-
53-
/**
54-
* A set of strings that identify the general purpose or important
55-
* characteristics of a given handler.
56-
*/
57-
tags?: Set<string>;
58-
}
59-
60-
/**
61-
* Builds a single handler function from zero or more middleware classes and a
62-
* core handler. The core handler is meant to send command objects to AWS
63-
* services and return promises that will resolve with the operation result or
64-
* be rejected with an error.
65-
*
66-
* When a composed handler is invoked, the arguments will pass through all
67-
* middleware in a defined order, and the return from the innermost handler will
68-
* pass through all middleware in the reverse of that order.
69-
*/
70-
export class MiddlewareStack<T extends Handler<any, any>> {
20+
export class MiddlewareStack<T extends Handler<any, any, any>> implements
21+
IMiddlewareStack<T>
22+
{
7123
private readonly entries: Array<HandlerListEntry<T>> = [];
7224
private sorted: boolean = false;
7325

74-
/**
75-
* Add middleware to the list, optionally specifying a step, priority, and
76-
* tags.
77-
*
78-
* Middleware registered at the same step and with the same priority may be
79-
* executed in any order.
80-
*/
8126
add(
8227
middleware: Middleware<T>,
8328
{
@@ -95,21 +40,12 @@ export class MiddlewareStack<T extends Handler<any, any>> {
9540
});
9641
}
9742

98-
/**
99-
* Create a shallow clone of this list. Step bindings and handler priorities
100-
* and tags are preserved in the copy.
101-
*/
10243
clone(): MiddlewareStack<T> {
10344
const clone = new MiddlewareStack<T>();
10445
clone.entries.push(...this.entries);
10546
return clone;
10647
}
10748

108-
/**
109-
* Create a list containing the middlewares in this list as well as the
110-
* middlewares in the `from` list. Neither source is modified, and step
111-
* bindings and handler priorities and tags are preserved in the copy.
112-
*/
11349
concat(
11450
from: MiddlewareStack<T>
11551
): MiddlewareStack<T> {
@@ -118,14 +54,6 @@ export class MiddlewareStack<T extends Handler<any, any>> {
11854
return clone;
11955
}
12056

121-
/**
122-
* Removes middleware from the stack.
123-
*
124-
* If a string is provided, any entry in the stack whose tags contain that
125-
* string will be removed from the stack.
126-
*
127-
* If a middleware class is provided, all usages thereof will be removed.
128-
*/
12957
remove(toRemove: Middleware<T>|string): boolean {
13058
const {length} = this.entries;
13159
if (typeof toRemove === 'string') {
@@ -137,16 +65,6 @@ export class MiddlewareStack<T extends Handler<any, any>> {
13765
return this.entries.length < length;
13866
}
13967

140-
/**
141-
* Builds a single handler function from zero or more middleware classes and
142-
* a core handler. The core handler is meant to send command objects to AWS
143-
* services and return promises that will resolve with the operation result
144-
* or be rejected with an error.
145-
*
146-
* When a composed handler is invoked, the arguments will pass through all
147-
* middleware in a defined order, and the return from the innermost handler
148-
* will pass through all middleware in the reverse of that order.
149-
*/
15068
resolve(handler: T, context: HandlerExecutionContext): T {
15169
if (!this.sorted) {
15270
this.sort();

packages/types/src/configuration.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import {MiddlewareStack, Step} from './middleware';
2+
3+
export type ConfigurationPropertyType = 'transient'|'persistent';
4+
5+
export interface ConfigurationPropertyDefinition<
6+
InputType,
7+
ServiceConfiguration extends {[key: string]: any}
8+
> {
9+
/**
10+
* The type of configuration property represented by this key.
11+
*
12+
* Transient properties are used during client construction, typically as
13+
* inputs to other configuration properties.
14+
*
15+
* Persistent properties will be stored on the `config` property of a
16+
* constructed client and will be used while executing methods.
17+
*/
18+
type: ConfigurationPropertyType;
19+
20+
/**
21+
* Whether this property must be supplied by the user of a client. If value
22+
* must be resolved but a default is available, this property should be
23+
* `false`.
24+
*/
25+
required: boolean;
26+
27+
/**
28+
* A static value to use as the default should none be supplied.
29+
*/
30+
defaultValue?: InputType;
31+
32+
/**
33+
* A function that returns a default value for this property. It will be
34+
* called if no value is supplied.
35+
*/
36+
defaultProvider?: {
37+
(config: ServiceConfiguration): InputType;
38+
}
39+
40+
/**
41+
* A function that finalizes the value supplied and/or alters the client
42+
* configuration or middleware stack in reaction to it.
43+
*/
44+
apply?: {
45+
(
46+
value: InputType,
47+
config: ServiceConfiguration,
48+
clientMiddlewareStack: MiddlewareStack<any>
49+
): void;
50+
}
51+
}
52+
53+
/**
54+
* A map of configuration property names to configuration property definitions.
55+
*/
56+
export type ConfigurationDefinition<T extends {[key: string]: any}> = {
57+
readonly [P in keyof T]: ConfigurationPropertyDefinition<P, T>;
58+
};

packages/types/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './abort';
2+
export * from './configuration';
23
export * from './credentials';
34
export * from './crypto';
45
export * from './http';

packages/types/src/middleware.ts

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface Handler<
4343
/**
4444
* A constructor for a class that implements the {Handler} interface.
4545
*/
46-
export interface Middleware<T extends Handler<any, any>> {
46+
export interface Middleware<T extends Handler<any, any, any>> {
4747
/**
4848
* @param next The handler to invoke after this middleware has operated on
4949
* the user input and before this middleware operates on the output.
@@ -56,6 +56,96 @@ export interface Middleware<T extends Handler<any, any>> {
5656
): T;
5757
}
5858

59+
export type Step = 'initialize'|'build'|'finalize';
60+
61+
export interface HandlerOptions {
62+
/**
63+
* Handlers are ordered using a "step" that describes the stage of command
64+
* execution at which the handler will be executed. The available steps are:
65+
*
66+
* - initialize: The input is being prepared and validated. Examples of
67+
* typical initialization tasks include injecting default options and
68+
* parameter validation.
69+
* - build: The input is being serialized into an HTTP request. The input
70+
* should not be altered in middleware occupying this step, as it may
71+
* have already been serialized into a request. Examples of typical
72+
* build tasks include request construction and injecting HTTP headers.
73+
* - finalize: The request is being prepared to be sent over the wire. The
74+
* request in this stage should already be semantically complete and
75+
* should therefore only be altered as match the recipient's
76+
* expectations. Examples of typical finalization tasks include request
77+
* signing and injecting hop-by-hop headers.
78+
*
79+
* Unlike initialization and build handlers, which are executed once
80+
* per operation execution, finalization handlers will be executed for
81+
* each HTTP request sent.
82+
*
83+
* @default 'build'
84+
*/
85+
step?: Step;
86+
87+
/**
88+
* A number that specifies how early in a given step of the middleware stack
89+
* a handler should be executed. Higher numeric priorities will be executed
90+
* earlier.
91+
*
92+
* @default 0
93+
*/
94+
priority?: number;
95+
96+
/**
97+
* A set of strings that identify the general purpose or important
98+
* characteristics of a given handler.
99+
*/
100+
tags?: Set<string>;
101+
}
102+
103+
export interface MiddlewareStack<T extends Handler<any, any, any>> {
104+
/**
105+
* Add middleware to the list, optionally specifying a step, priority, and
106+
* tags.
107+
*
108+
* Middleware registered at the same step and with the same priority may be
109+
* executed in any order.
110+
*/
111+
add(middleware: Middleware<T>, options?: HandlerOptions): void;
112+
113+
/**
114+
* Create a shallow clone of this list. Step bindings and handler priorities
115+
* and tags are preserved in the copy.
116+
*/
117+
clone(): MiddlewareStack<T>;
118+
119+
/**
120+
* Create a list containing the middlewares in this list as well as the
121+
* middlewares in the `from` list. Neither source is modified, and step
122+
* bindings and handler priorities and tags are preserved in the copy.
123+
*/
124+
concat(from: MiddlewareStack<T>): MiddlewareStack<T>;
125+
126+
/**
127+
* Removes middleware from the stack.
128+
*
129+
* If a string is provided, any entry in the stack whose tags contain that
130+
* string will be removed from the stack.
131+
*
132+
* If a middleware class is provided, all usages thereof will be removed.
133+
*/
134+
remove(toRemove: Middleware<T>|string): boolean;
135+
136+
/**
137+
* Builds a single handler function from zero or more middleware classes and
138+
* a core handler. The core handler is meant to send command objects to AWS
139+
* services and return promises that will resolve with the operation result
140+
* or be rejected with an error.
141+
*
142+
* When a composed handler is invoked, the arguments will pass through all
143+
* middleware in a defined order, and the return from the innermost handler
144+
* will pass through all middleware in the reverse of that order.
145+
*/
146+
resolve(handler: T, context: HandlerExecutionContext): T;
147+
}
148+
59149
/**
60150
* Data and helper objects that are not expected to change from one execution of
61151
* a composed handler to another.

0 commit comments

Comments
 (0)