UNPKG

17.3 kBTypeScriptView Raw
1import { AuthScheme, HttpAuthDefinition } from "./auth";
2import { EndpointV2 } from "./endpoint";
3import { Logger } from "./logger";
4import { UserAgent } from "./util";
5/**
6 * @public
7 */
8export interface InitializeHandlerArguments<Input extends object> {
9 /**
10 * User input to a command. Reflects the userland representation of the
11 * union of data types the command can effectively handle.
12 */
13 input: Input;
14}
15/**
16 * @public
17 */
18export interface InitializeHandlerOutput<Output extends object> extends DeserializeHandlerOutput<Output> {
19 output: Output;
20}
21/**
22 * @public
23 */
24export interface SerializeHandlerArguments<Input extends object> extends InitializeHandlerArguments<Input> {
25 /**
26 * The user input serialized as a request object. The request object is unknown,
27 * so you cannot modify it directly. When work with request, you need to guard its
28 * type to e.g. HttpRequest with 'instanceof' operand
29 *
30 * During the build phase of the execution of a middleware stack, a built
31 * request may or may not be available.
32 */
33 request?: unknown;
34}
35/**
36 * @public
37 */
38export interface SerializeHandlerOutput<Output extends object> extends InitializeHandlerOutput<Output> {
39}
40/**
41 * @public
42 */
43export interface BuildHandlerArguments<Input extends object> extends FinalizeHandlerArguments<Input> {
44}
45/**
46 * @public
47 */
48export interface BuildHandlerOutput<Output extends object> extends InitializeHandlerOutput<Output> {
49}
50/**
51 * @public
52 */
53export interface FinalizeHandlerArguments<Input extends object> extends SerializeHandlerArguments<Input> {
54 /**
55 * The user input serialized as a request.
56 */
57 request: unknown;
58}
59/**
60 * @public
61 */
62export interface FinalizeHandlerOutput<Output extends object> extends InitializeHandlerOutput<Output> {
63}
64/**
65 * @public
66 */
67export interface DeserializeHandlerArguments<Input extends object> extends FinalizeHandlerArguments<Input> {
68}
69/**
70 * @public
71 */
72export interface DeserializeHandlerOutput<Output extends object> {
73 /**
74 * The raw response object from runtime is deserialized to structured output object.
75 * The response object is unknown so you cannot modify it directly. When work with
76 * response, you need to guard its type to e.g. HttpResponse with 'instanceof' operand.
77 *
78 * During the deserialize phase of the execution of a middleware stack, a deserialized
79 * response may or may not be available
80 */
81 response: unknown;
82 output?: Output;
83}
84/**
85 * @public
86 */
87export interface InitializeHandler<Input extends object, Output extends object> {
88 /**
89 * Asynchronously converts an input object into an output object.
90 *
91 * @param args - An object containing a input to the command as well as any
92 * associated or previously generated execution artifacts.
93 */
94 (args: InitializeHandlerArguments<Input>): Promise<InitializeHandlerOutput<Output>>;
95}
96/**
97 * @public
98 */
99export type Handler<Input extends object, Output extends object> = InitializeHandler<Input, Output>;
100/**
101 * @public
102 */
103export interface SerializeHandler<Input extends object, Output extends object> {
104 /**
105 * Asynchronously converts an input object into an output object.
106 *
107 * @param args - An object containing a input to the command as well as any
108 * associated or previously generated execution artifacts.
109 */
110 (args: SerializeHandlerArguments<Input>): Promise<SerializeHandlerOutput<Output>>;
111}
112/**
113 * @public
114 */
115export interface FinalizeHandler<Input extends object, Output extends object> {
116 /**
117 * Asynchronously converts an input object into an output object.
118 *
119 * @param args - An object containing a input to the command as well as any
120 * associated or previously generated execution artifacts.
121 */
122 (args: FinalizeHandlerArguments<Input>): Promise<FinalizeHandlerOutput<Output>>;
123}
124/**
125 * @public
126 */
127export interface BuildHandler<Input extends object, Output extends object> {
128 (args: BuildHandlerArguments<Input>): Promise<BuildHandlerOutput<Output>>;
129}
130/**
131 * @public
132 */
133export interface DeserializeHandler<Input extends object, Output extends object> {
134 (args: DeserializeHandlerArguments<Input>): Promise<DeserializeHandlerOutput<Output>>;
135}
136/**
137 * @public
138 *
139 * A factory function that creates functions implementing the `Handler`
140 * interface.
141 */
142export interface InitializeMiddleware<Input extends object, Output extends object> {
143 /**
144 * @param next - The handler to invoke after this middleware has operated on
145 * the user input and before this middleware operates on the output.
146 *
147 * @param context - Invariant data and functions for use by the handler.
148 */
149 (next: InitializeHandler<Input, Output>, context: HandlerExecutionContext): InitializeHandler<Input, Output>;
150}
151/**
152 * @public
153 *
154 * A factory function that creates functions implementing the `BuildHandler`
155 * interface.
156 */
157export interface SerializeMiddleware<Input extends object, Output extends object> {
158 /**
159 * @param next - The handler to invoke after this middleware has operated on
160 * the user input and before this middleware operates on the output.
161 *
162 * @param context - Invariant data and functions for use by the handler.
163 */
164 (next: SerializeHandler<Input, Output>, context: HandlerExecutionContext): SerializeHandler<Input, Output>;
165}
166/**
167 * @public
168 *
169 * A factory function that creates functions implementing the `FinalizeHandler`
170 * interface.
171 */
172export interface FinalizeRequestMiddleware<Input extends object, Output extends object> {
173 /**
174 * @param next - The handler to invoke after this middleware has operated on
175 * the user input and before this middleware operates on the output.
176 *
177 * @param context - Invariant data and functions for use by the handler.
178 */
179 (next: FinalizeHandler<Input, Output>, context: HandlerExecutionContext): FinalizeHandler<Input, Output>;
180}
181/**
182 * @public
183 */
184export interface BuildMiddleware<Input extends object, Output extends object> {
185 (next: BuildHandler<Input, Output>, context: HandlerExecutionContext): BuildHandler<Input, Output>;
186}
187/**
188 * @public
189 */
190export interface DeserializeMiddleware<Input extends object, Output extends object> {
191 (next: DeserializeHandler<Input, Output>, context: HandlerExecutionContext): DeserializeHandler<Input, Output>;
192}
193/**
194 * @public
195 */
196export type MiddlewareType<Input extends object, Output extends object> = InitializeMiddleware<Input, Output> | SerializeMiddleware<Input, Output> | BuildMiddleware<Input, Output> | FinalizeRequestMiddleware<Input, Output> | DeserializeMiddleware<Input, Output>;
197/**
198 * @public
199 *
200 * A factory function that creates the terminal handler atop which a middleware
201 * stack sits.
202 */
203export interface Terminalware {
204 <Input extends object, Output extends object>(context: HandlerExecutionContext): DeserializeHandler<Input, Output>;
205}
206/**
207 * @public
208 */
209export type Step = "initialize" | "serialize" | "build" | "finalizeRequest" | "deserialize";
210/**
211 * @public
212 */
213export type Priority = "high" | "normal" | "low";
214/**
215 * @public
216 */
217export interface HandlerOptions {
218 /**
219 * Handlers are ordered using a "step" that describes the stage of command
220 * execution at which the handler will be executed. The available steps are:
221 *
222 * - initialize: The input is being prepared. Examples of typical
223 * initialization tasks include injecting default options computing
224 * derived parameters.
225 * - serialize: The input is complete and ready to be serialized. Examples
226 * of typical serialization tasks include input validation and building
227 * an HTTP request from user input.
228 * - build: The input has been serialized into an HTTP request, but that
229 * request may require further modification. Any request alterations
230 * will be applied to all retries. Examples of typical build tasks
231 * include injecting HTTP headers that describe a stable aspect of the
232 * request, such as `Content-Length` or a body checksum.
233 * - finalizeRequest: The request is being prepared to be sent over the wire. The
234 * request in this stage should already be semantically complete and
235 * should therefore only be altered as match the recipient's
236 * expectations. Examples of typical finalization tasks include request
237 * signing and injecting hop-by-hop headers.
238 * - deserialize: The response has arrived, the middleware here will deserialize
239 * the raw response object to structured response
240 *
241 * Unlike initialization and build handlers, which are executed once
242 * per operation execution, finalization and deserialize handlers will be
243 * executed foreach HTTP request sent.
244 *
245 * @defaultValue 'initialize'
246 */
247 step?: Step;
248 /**
249 * A list of strings to any that identify the general purpose or important
250 * characteristics of a given handler.
251 */
252 tags?: Array<string>;
253 /**
254 * A unique name to refer to a middleware
255 */
256 name?: string;
257 /**
258 * A flag to override the existing middleware with the same name. Without
259 * setting it, adding middleware with duplicated name will throw an exception.
260 * @internal
261 */
262 override?: boolean;
263}
264/**
265 * @public
266 */
267export interface AbsoluteLocation {
268 /**
269 * By default middleware will be added to individual step in un-guaranteed order.
270 * In the case that
271 *
272 * @defaultValue 'normal'
273 */
274 priority?: Priority;
275}
276/**
277 * @public
278 */
279export type Relation = "before" | "after";
280/**
281 * @public
282 */
283export interface RelativeLocation {
284 /**
285 * Specify the relation to be before or after a know middleware.
286 */
287 relation: Relation;
288 /**
289 * A known middleware name to indicate inserting middleware's location.
290 */
291 toMiddleware: string;
292}
293/**
294 * @public
295 */
296export type RelativeMiddlewareOptions = RelativeLocation & Omit<HandlerOptions, "step">;
297/**
298 * @public
299 */
300export interface InitializeHandlerOptions extends HandlerOptions {
301 step?: "initialize";
302}
303/**
304 * @public
305 */
306export interface SerializeHandlerOptions extends HandlerOptions {
307 step: "serialize";
308}
309/**
310 * @public
311 */
312export interface BuildHandlerOptions extends HandlerOptions {
313 step: "build";
314}
315/**
316 * @public
317 */
318export interface FinalizeRequestHandlerOptions extends HandlerOptions {
319 step: "finalizeRequest";
320}
321/**
322 * @public
323 */
324export interface DeserializeHandlerOptions extends HandlerOptions {
325 step: "deserialize";
326}
327/**
328 * @public
329 *
330 * A stack storing middleware. It can be resolved into a handler. It supports 2
331 * approaches for adding middleware:
332 * 1. Adding middleware to specific step with `add()`. The order of middleware
333 * added into same step is determined by order of adding them. If one middleware
334 * needs to be executed at the front of the step or at the end of step, set
335 * `priority` options to `high` or `low`.
336 * 2. Adding middleware to location relative to known middleware with `addRelativeTo()`.
337 * This is useful when given middleware must be executed before or after specific
338 * middleware(`toMiddleware`). You can add a middleware relatively to another
339 * middleware which also added relatively. But eventually, this relative middleware
340 * chain **must** be 'anchored' by a middleware that added using `add()` API
341 * with absolute `step` and `priority`. This mothod will throw if specified
342 * `toMiddleware` is not found.
343 */
344export interface MiddlewareStack<Input extends object, Output extends object> extends Pluggable<Input, Output> {
345 /**
346 * Add middleware to the stack to be executed during the "initialize" step,
347 * optionally specifying a priority, tags and name
348 */
349 add(middleware: InitializeMiddleware<Input, Output>, options?: InitializeHandlerOptions & AbsoluteLocation): void;
350 /**
351 * Add middleware to the stack to be executed during the "serialize" step,
352 * optionally specifying a priority, tags and name
353 */
354 add(middleware: SerializeMiddleware<Input, Output>, options: SerializeHandlerOptions & AbsoluteLocation): void;
355 /**
356 * Add middleware to the stack to be executed during the "build" step,
357 * optionally specifying a priority, tags and name
358 */
359 add(middleware: BuildMiddleware<Input, Output>, options: BuildHandlerOptions & AbsoluteLocation): void;
360 /**
361 * Add middleware to the stack to be executed during the "finalizeRequest" step,
362 * optionally specifying a priority, tags and name
363 */
364 add(middleware: FinalizeRequestMiddleware<Input, Output>, options: FinalizeRequestHandlerOptions & AbsoluteLocation): void;
365 /**
366 * Add middleware to the stack to be executed during the "deserialize" step,
367 * optionally specifying a priority, tags and name
368 */
369 add(middleware: DeserializeMiddleware<Input, Output>, options: DeserializeHandlerOptions & AbsoluteLocation): void;
370 /**
371 * Add middleware to a stack position before or after a known middleware,optionally
372 * specifying name and tags.
373 */
374 addRelativeTo(middleware: MiddlewareType<Input, Output>, options: RelativeMiddlewareOptions): void;
375 /**
376 * Apply a customization function to mutate the middleware stack, often
377 * used for customizations that requires mutating multiple middleware.
378 */
379 use(pluggable: Pluggable<Input, Output>): void;
380 /**
381 * Create a shallow clone of this stack. Step bindings and handler priorities
382 * and tags are preserved in the copy.
383 */
384 clone(): MiddlewareStack<Input, Output>;
385 /**
386 * Removes middleware from the stack.
387 *
388 * If a string is provided, it will be treated as middleware name. If a middleware
389 * is inserted with the given name, it will be removed.
390 *
391 * If a middleware class is provided, all usages thereof will be removed.
392 */
393 remove(toRemove: MiddlewareType<Input, Output> | string): boolean;
394 /**
395 * Removes middleware that contains given tag
396 *
397 * Multiple middleware will potentially be removed
398 */
399 removeByTag(toRemove: string): boolean;
400 /**
401 * Create a stack containing the middlewares in this stack as well as the
402 * middlewares in the `from` stack. Neither source is modified, and step
403 * bindings and handler priorities and tags are preserved in the copy.
404 */
405 concat<InputType extends Input, OutputType extends Output>(from: MiddlewareStack<InputType, OutputType>): MiddlewareStack<InputType, OutputType>;
406 /**
407 * Returns a list of the current order of middleware in the stack.
408 * This does not execute the middleware functions, nor does it
409 * provide a reference to the stack itself.
410 */
411 identify(): string[];
412 /**
413 * Builds a single handler function from zero or more middleware classes and
414 * a core handler. The core handler is meant to send command objects to AWS
415 * services and return promises that will resolve with the operation result
416 * or be rejected with an error.
417 *
418 * When a composed handler is invoked, the arguments will pass through all
419 * middleware in a defined order, and the return from the innermost handler
420 * will pass through all middleware in the reverse of that order.
421 */
422 resolve<InputType extends Input, OutputType extends Output>(handler: DeserializeHandler<InputType, OutputType>, context: HandlerExecutionContext): InitializeHandler<InputType, OutputType>;
423}
424/**
425 * @public
426 *
427 * Data and helper objects that are not expected to change from one execution of
428 * a composed handler to another.
429 */
430export interface HandlerExecutionContext {
431 /**
432 * A logger that may be invoked by any handler during execution of an
433 * operation.
434 */
435 logger?: Logger;
436 /**
437 * Additional user agent that inferred by middleware. It can be used to save
438 * the internal user agent sections without overriding the `customUserAgent`
439 * config in clients.
440 */
441 userAgent?: UserAgent;
442 /**
443 * Resolved by the endpointMiddleware function of `@aws-sdk/middleware-endpoint`
444 * in the serialization stage.
445 */
446 endpointV2?: EndpointV2;
447 /**
448 * Set at the same time as endpointV2.
449 */
450 authSchemes?: AuthScheme[];
451 /**
452 * The current auth configuration that has been set by any auth middleware and
453 * that will prevent from being set more than once.
454 */
455 currentAuthConfig?: HttpAuthDefinition;
456 /**
457 * Used by DynamoDbDocumentClient.
458 */
459 dynamoDbDocumentClientOptions?: Partial<{
460 overrideInputFilterSensitiveLog(...args: any[]): string | void;
461 overrideOutputFilterSensitiveLog(...args: any[]): string | void;
462 }>;
463 [key: string]: any;
464}
465/**
466 * @public
467 */
468export interface Pluggable<Input extends object, Output extends object> {
469 /**
470 * A function that mutate the passed in middleware stack. Functions implementing
471 * this interface can add, remove, modify existing middleware stack from clients
472 * or commands
473 */
474 applyToStack: (stack: MiddlewareStack<Input, Output>) => void;
475}