Skip to content

Commit

Permalink
reorganize core and smithy-client
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Oct 9, 2024
1 parent a7e6a52 commit 840e922
Show file tree
Hide file tree
Showing 21 changed files with 147 additions and 117 deletions.
9 changes: 9 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
"import": "./dist-es/submodules/cbor/index.js",
"require": "./dist-cjs/submodules/cbor/index.js",
"types": "./dist-types/submodules/cbor/index.d.ts"
},
"./protocols": {
"module": "./dist-es/submodules/protocols/index.js",
"node": "./dist-cjs/submodules/protocols/index.js",
"import": "./dist-es/submodules/protocols/index.js",
"require": "./dist-cjs/submodules/protocols/index.js",
"types": "./dist-types/submodules/protocols/index.d.ts"
}
},
"author": {
Expand Down Expand Up @@ -68,6 +75,8 @@
"files": [
"./cbor.d.ts",
"./cbor.js",
"./protocols.d.ts",
"./protocols.js",
"dist-*/**"
],
"homepage": "https://github.com/awslabs/smithy-typescript/tree/main/packages/core",
Expand Down
7 changes: 7 additions & 0 deletions packages/core/protocols.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Do not edit:
* This is a compatibility redirect for contexts that do not understand package.json exports field.
*/
declare module "@smithy/core/protocols" {
export * from "@smithy/core/dist-types/submodules/protocols/index.d";
}
6 changes: 6 additions & 0 deletions packages/core/protocols.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

/**
* Do not edit:
* This is a compatibility redirect for contexts that do not understand package.json exports field.
*/
module.exports = require("./dist-cjs/submodules/protocols/index.js");
3 changes: 0 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ export * from "./middleware-http-auth-scheme";
export * from "./middleware-http-signing";
export * from "./normalizeProvider";
export { createPaginator } from "./pagination/createPaginator";
export * from "./protocols/collect-stream-body";
export * from "./protocols/requestBuilder";
export * from "./protocols/resolve-path";
export * from "./protocols/extended-encode-uri-component";
export * from "./setFeature";
export * from "./util-identity-and-auth";
108 changes: 2 additions & 106 deletions packages/core/src/protocols/requestBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,109 +1,5 @@
import { HttpRequest } from "@smithy/protocol-http";
import type { SerdeContext } from "@smithy/types";

import { resolvedPath } from "./resolve-path";

/**
* @internal
* used in code-generated serde.
* Backwards compatibility re-export.
*/
export function requestBuilder(input: any, context: SerdeContext): RequestBuilder {
return new RequestBuilder(input, context);
}

/**
* @internal
*/
export class RequestBuilder {
private query: Record<string, string> = {};
private method = "";
private headers: Record<string, string> = {};
private path = "";
private body: any = null;
private hostname = "";

private resolvePathStack: Array<(path: string) => void> = [];

public constructor(
private input: any,
private context: SerdeContext
) {}

public async build() {
const { hostname, protocol = "https", port, path: basePath } = await this.context.endpoint();
this.path = basePath;
for (const resolvePath of this.resolvePathStack) {
resolvePath(this.path);
}
return new HttpRequest({
protocol,
hostname: this.hostname || hostname,
port,
method: this.method,
path: this.path,
query: this.query,
body: this.body,
headers: this.headers,
});
}

/**
* Brevity setter for "hostname".
*/
public hn(hostname: string) {
this.hostname = hostname;
return this;
}

/**
* Brevity initial builder for "basepath".
*/
public bp(uriLabel: string) {
this.resolvePathStack.push((basePath: string) => {
this.path = `${basePath?.endsWith("/") ? basePath.slice(0, -1) : basePath || ""}` + uriLabel;
});
return this;
}

/**
* Brevity incremental builder for "path".
*/
public p(memberName: string, labelValueProvider: () => string | undefined, uriLabel: string, isGreedyLabel: boolean) {
this.resolvePathStack.push((path: string) => {
this.path = resolvedPath(path, this.input, memberName, labelValueProvider, uriLabel, isGreedyLabel);
});
return this;
}

/**
* Brevity setter for "headers".
*/
public h(headers: Record<string, string>) {
this.headers = headers;
return this;
}

/**
* Brevity setter for "query".
*/
public q(query: Record<string, string>) {
this.query = query;
return this;
}

/**
* Brevity setter for "body".
*/
public b(body: any) {
this.body = body;
return this;
}

/**
* Brevity setter for "method".
*/
public m(method: string) {
this.method = method;
return this;
}
}
export { requestBuilder } from "@smithy/core/protocols";
2 changes: 1 addition & 1 deletion packages/core/src/submodules/cbor/parseCborBody.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { collectBody } from "@smithy/core";
import { collectBody } from "@smithy/core/protocols";
import { HttpRequest as __HttpRequest } from "@smithy/protocol-http";
import { HeaderBag as __HeaderBag, HttpResponse, SerdeContext as __SerdeContext, SerdeContext } from "@smithy/types";
import { calculateBodyLength } from "@smithy/util-body-length-browser";
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/submodules/protocols/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./collect-stream-body";
export * from "./extended-encode-uri-component";
export * from "./requestBuilder";
export * from "./resolve-path";
108 changes: 108 additions & 0 deletions packages/core/src/submodules/protocols/requestBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { resolvedPath } from "@smithy/core/protocols";
import { HttpRequest } from "@smithy/protocol-http";
import type { SerdeContext } from "@smithy/types";

/**
* @internal
* used in code-generated serde.
*/
export function requestBuilder(input: any, context: SerdeContext): RequestBuilder {
return new RequestBuilder(input, context);
}

/**
* @internal
*/
export class RequestBuilder {
private query: Record<string, string> = {};
private method = "";
private headers: Record<string, string> = {};
private path = "";
private body: any = null;
private hostname = "";

private resolvePathStack: Array<(path: string) => void> = [];

public constructor(
private input: any,
private context: SerdeContext
) {}

public async build() {
const { hostname, protocol = "https", port, path: basePath } = await this.context.endpoint();
this.path = basePath;
for (const resolvePath of this.resolvePathStack) {
resolvePath(this.path);
}
return new HttpRequest({
protocol,
hostname: this.hostname || hostname,
port,
method: this.method,
path: this.path,
query: this.query,
body: this.body,
headers: this.headers,
});
}

/**
* Brevity setter for "hostname".
*/
public hn(hostname: string) {
this.hostname = hostname;
return this;
}

/**
* Brevity initial builder for "basepath".
*/
public bp(uriLabel: string) {
this.resolvePathStack.push((basePath: string) => {
this.path = `${basePath?.endsWith("/") ? basePath.slice(0, -1) : basePath || ""}` + uriLabel;
});
return this;
}

/**
* Brevity incremental builder for "path".
*/
public p(memberName: string, labelValueProvider: () => string | undefined, uriLabel: string, isGreedyLabel: boolean) {
this.resolvePathStack.push((path: string) => {
this.path = resolvedPath(path, this.input, memberName, labelValueProvider, uriLabel, isGreedyLabel);
});
return this;
}

/**
* Brevity setter for "headers".
*/
public h(headers: Record<string, string>) {
this.headers = headers;
return this;
}

/**
* Brevity setter for "query".
*/
public q(query: Record<string, string>) {
this.query = query;
return this;
}

/**
* Brevity setter for "body".
*/
public b(body: any) {
this.body = body;
return this;
}

/**
* Brevity setter for "method".
*/
public m(method: string) {
this.method = method;
return this;
}
}
File renamed without changes.
3 changes: 2 additions & 1 deletion packages/core/tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"outDir": "dist-cjs",
"rootDir": "src",
"paths": {
"@smithy/core/cbor": ["./src/submodules/cbor/index.ts"]
"@smithy/core/cbor": ["./src/submodules/cbor/index.ts"],
"@smithy/core/protocols": ["./src/submodules/protocols/index.ts"]
}
},
"extends": "../../tsconfig.cjs.json",
Expand Down
3 changes: 2 additions & 1 deletion packages/core/tsconfig.es.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"outDir": "dist-es",
"rootDir": "src",
"paths": {
"@smithy/core/cbor": ["./src/submodules/cbor/index.ts"]
"@smithy/core/cbor": ["./src/submodules/cbor/index.ts"],
"@smithy/core/protocols": ["./src/submodules/protocols/index.ts"]
}
},
"extends": "../../tsconfig.es.json",
Expand Down
3 changes: 2 additions & 1 deletion packages/core/tsconfig.types.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"declarationDir": "dist-types",
"rootDir": "src",
"paths": {
"@smithy/core/cbor": ["./src/submodules/cbor/index.ts"]
"@smithy/core/cbor": ["./src/submodules/cbor/index.ts"],
"@smithy/core/protocols": ["./src/submodules/protocols/index.ts"]
}
},
"extends": "../../tsconfig.types.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const compressionMiddleware =
updatedHeaders = { ...headers, "content-encoding": algorithm };
}

if (headers["content-encoding"].includes("gzip")) {
if (updatedHeaders["content-encoding"].includes("gzip")) {
setFeature(context, "GZIP_REQUEST_COMPRESSION", "L");
}

Expand Down
2 changes: 1 addition & 1 deletion packages/smithy-client/src/collect-stream-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
* @internal
* Backwards compatibility re-export.
*/
export { collectBody } from "@smithy/core";
export { collectBody } from "@smithy/core/protocols";
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
* @internal
* Backwards compatibility re-export.
*/
export { extendedEncodeURIComponent } from "@smithy/core";
export { extendedEncodeURIComponent } from "@smithy/core/protocols";
2 changes: 1 addition & 1 deletion packages/smithy-client/src/resolve-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
* @internal
* Backwards compatibility re-export.
*/
export { resolvedPath } from "@smithy/core";
export { resolvedPath } from "@smithy/core/protocols";

0 comments on commit 840e922

Please sign in to comment.