Skip to content

Commit c0e8a6f

Browse files
committed
feat: wip
1 parent e03a10a commit c0e8a6f

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

.changeset/strong-baboons-sort.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/core": minor
3+
---
4+
5+
add type interfaces for runtime model interpreters

packages/core/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ export * from "./getSmithyContext";
55
export * from "./normalizeProvider";
66
export * from "./protocols/requestBuilder";
77
export { createPaginator } from "./pagination/createPaginator";
8+
export * from "./protocols/serde/SmithyModel";
9+
export * from "./protocols/serde/RuntimeModelInterpreter";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { HttpRequest as IHttpRequest, HttpResponse as IHttpResponse, SerdeContext } from "@smithy/types";
2+
3+
import { ISmithyModel, ISmithyModelShape, ISmithyModelShapeId, SmithyModel } from "./SmithyModel";
4+
5+
export interface IRuntimeModelInterpreter {}
6+
7+
export abstract class RuntimeModelInterpreter implements IRuntimeModelInterpreter {
8+
public model: SmithyModel;
9+
10+
protected constructor(model: ISmithyModel) {
11+
this.model = SmithyModel.from(model);
12+
}
13+
14+
public abstract serialize<I>(
15+
input: I,
16+
requestShapeId: ISmithyModelShapeId,
17+
context: SerdeContext
18+
): Promise<IHttpRequest>;
19+
20+
public abstract deserialize<O>(
21+
httpResponse: IHttpResponse,
22+
responseShapeId: ISmithyModelShapeId,
23+
context: SerdeContext
24+
): Promise<O>;
25+
26+
public getShape<T extends ISmithyModelShape>(id: ISmithyModelShapeId): T {
27+
const shape = this.model.shapes[id];
28+
if (!shape) {
29+
throw new Error("shape not found: " + id);
30+
}
31+
return shape as T;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
export interface ISmithyModel {
2+
smithy: "2.0";
3+
metadata: {
4+
suppressions: { id: string; namespace: string }[];
5+
};
6+
shapes: Record<ISmithyModelShapeId, ISmithyModelShape>;
7+
}
8+
9+
export interface ISmithyModelShape {
10+
type: string;
11+
}
12+
13+
export interface ISmithyModelServiceShape extends ISmithyModelShape {
14+
type: "service";
15+
version: string;
16+
operations: { target: ISmithyModelShapeId }[];
17+
traits: ISmithyModelTraits;
18+
}
19+
20+
export interface ISmithyModelStructureShape extends ISmithyModelShape {
21+
type: "structure";
22+
members: {
23+
[memberName: string]: {
24+
target: ISmithyModelShapeId;
25+
traits: ISmithyModelTraits;
26+
};
27+
};
28+
traits: ISmithyModelTraits;
29+
}
30+
31+
export interface ISmithyModelOperationShape extends ISmithyModelShape {
32+
type: "operation";
33+
input: {
34+
target: ISmithyModelShapeId;
35+
};
36+
output: {
37+
target: ISmithyModelShapeId;
38+
};
39+
errors: { target: ISmithyModelShapeId }[];
40+
traits: ISmithyModelTraits;
41+
}
42+
43+
export type ISmithyModelTraits = {
44+
"smithy.api#http"?: {
45+
method: string;
46+
uri: string;
47+
code: number;
48+
};
49+
"smithy.api#httpLabel"?: {} | undefined;
50+
"smithy.api#httpHeader"?: string;
51+
"smithy.api#httpQuery"?: string;
52+
"smithy.api#httpPayload"?: {} | undefined;
53+
"smithy.api#jsonName"?: string;
54+
"smithy.api#httpResponseCode"?: {} | undefined;
55+
[traitName: string]: unknown;
56+
};
57+
58+
export type ISmithyModelShapeId = `${string}#${string}`;
59+
60+
export class SmithyModel implements ISmithyModel {
61+
public smithy: ISmithyModel["smithy"];
62+
public metadata: ISmithyModel["metadata"];
63+
public shapes: ISmithyModel["shapes"];
64+
65+
public static from(model: ISmithyModel) {
66+
if (model instanceof SmithyModel) {
67+
return model;
68+
}
69+
return new SmithyModel(model);
70+
}
71+
72+
public constructor(public readonly _jsonObject: ISmithyModel) {
73+
this.smithy = _jsonObject.smithy;
74+
this.metadata = _jsonObject.metadata;
75+
this.shapes = _jsonObject.shapes;
76+
}
77+
}

0 commit comments

Comments
 (0)