Skip to content

Commit 2ef0036

Browse files
change error handling, add README, and re-add constructors
1 parent da20ede commit 2ef0036

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1203
-285
lines changed

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,46 @@
1-
# speakeasy-client-sdk-typescript
1+
# speakeasy-client-sdk-typescript
2+
3+
This is the Speakeasy API Client SDK for Typescript. It is generated from our OpenAPI spec found at https://docs.speakeasyapi.dev/openapi.yaml and used for interacting with the [Speakeasy API](https://docs.speakeasyapi.dev/docs/speakeasy-api/speakeasy-api).
4+
5+
This SDK was generated using Speakeasy's SDK Generator. For more information on how to use the generator to generate your own SDKs, please see the [Speakeasy Client SDK Generator Docs](https://docs.speakeasyapi.dev/docs/using-speakeasy/client-sdks).
6+
7+
## Installation
8+
9+
```bash
10+
// Coming soon
11+
npm i --save speakeasy-client-sdk-typescript
12+
```
13+
14+
## Example usage
15+
```typescript
16+
// Replace relative paths below with npm module once it's published
17+
import {
18+
SDK,
19+
WithSecurity,
20+
} from "../../speakeasy-client-sdk-typescript/src/sdk/sdk";
21+
import {
22+
Security,
23+
SchemeApiKey,
24+
} from "../../speakeasy-client-sdk-typescript/src/sdk/models/shared/security";
25+
import {
26+
GetApisResponse,
27+
GetApisRequest,
28+
GetApisQueryParams,
29+
GetApisOp,
30+
} from "../../speakeasy-client-sdk-typescript/src/sdk/models/operations";
31+
import { AxiosError } from "axios";
32+
33+
const serverURL = "https://api.prod.speakeasyapi.dev";
34+
const security: Security = new Security(new SchemeApiKey("YOUR_API_KEY")); // Replace with your API key from your Speakeasy Workspace
35+
const sdk: SDK = new SDK(WithSecurity(serverURL, security));
36+
37+
const metadata: Map<string, string[]> = new Map([["label", ["1"]]]);
38+
const op: GetApisOp = new GetApisOp(true);
39+
40+
const request: GetApisRequest = new GetApisRequest(
41+
new GetApisQueryParams(metadata, op)
42+
);
43+
sdk.GetApis(request).then((res: GetApisResponse | AxiosError) => {
44+
console.log(res);
45+
});
46+
```

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"lint:fix": "tsc --noemit && eslint \"./src\" --ext .ts,.tsx --fix"
44
},
55
"dependencies": {
6+
"@types/node": "^18.11.5",
67
"@types/qs": "^6.9.7",
78
"axios": "^1.1.3",
89
"form-data": "^4.0.0",

src/internal/utils/contenttype.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,28 @@ export function MatchContentType(
22
contentType: string,
33
pattern: string
44
): boolean {
5-
if (contentType === pattern || pattern === "*" || pattern === "*/*")
6-
return true;
7-
if (contentType === pattern) return true;
8-
const parts: string[] = contentType.split("/");
9-
if (parts.length === 2) {
10-
if (`${parts[0]}/*` === pattern || `*/${parts[1]}` === pattern) return true;
11-
}
12-
return false;
5+
let res = false;
6+
contentType
7+
.split(";")
8+
.map((ctPart: string) => {
9+
return ctPart.trim();
10+
})
11+
.forEach((ctPart: string) => {
12+
if (ctPart === pattern || pattern === "*" || pattern === "*/*") {
13+
res = true;
14+
return;
15+
}
16+
if (ctPart === pattern) {
17+
res = true;
18+
return;
19+
}
20+
const parts: string[] = ctPart.split("/");
21+
if (parts.length === 2) {
22+
if (`${parts[0]}/*` === pattern || `*/${parts[1]}` === pattern) {
23+
res = true;
24+
return;
25+
}
26+
}
27+
});
28+
return res;
1329
}

src/internal/utils/requestbody.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import FormData from "form-data";
2+
import { createReadStream, writeFileSync } from "fs";
3+
import { join } from "path";
4+
import qs from "qs";
25

36
const requestMetadataKey = "request";
47
const mpFormMetadataKey = "multipart_form";
@@ -19,14 +22,20 @@ export function SerializeRequestBody(request: any): [object, any] {
1922
case "multipart/form-data":
2023
case "multipart/mixed":
2124
requestBody = EncodeMultipartFormData(request[fname]);
22-
requestHeaders = requestBody.getHeaders();
25+
requestHeaders = (requestBody as FormData).getHeaders();
26+
break;
27+
case "application/x-www-form-urlencoded":
28+
[requestHeaders, requestBody] = [
29+
{ "Content-Type": `${requestDecorator.MediaType}` },
30+
qs.stringify({ ...request[fname] }),
31+
];
2332
break;
2433
case "application/json":
2534
case "text/json":
2635
default:
2736
[requestHeaders, requestBody] = [
2837
{ "Content-Type": `${requestDecorator.MediaType}` },
29-
request[fname],
38+
{ ...request[fname] },
3039
];
3140
}
3241
});
@@ -48,29 +57,29 @@ function EncodeMultipartFormData(form: any): FormData {
4857
if (mpFormDecorator.File)
4958
return EncodeMultipartFormDataFile(formData, form[fname]);
5059
else if (mpFormDecorator.JSON) {
51-
formData.append(mpFormDecorator.Name, JSON.stringify(form[fname]));
60+
formData.append(mpFormDecorator.Name, { ...form[fname] });
5261
} else {
53-
// let formData: FormData = new FormData();
54-
// const val: unknown = form[fname];
55-
// if (Array.isArray(form[fname]))
56-
// formData.append(mpFormDecorator.Name + "[]", val);
57-
// else
58-
// formData.append(mpFormDecorator.Name, val)
62+
if (Array.isArray(form[fname])) {
63+
form[fname].forEach((val: any) => {
64+
formData.append(mpFormDecorator.Name + "[]", JSON.stringify(val));
65+
});
66+
} else {
67+
formData.append(mpFormDecorator.Name, JSON.stringify(form[fname]));
68+
}
5969
}
6070
});
6171
return formData;
6272
}
6373

64-
function EncodeMultipartFormDataFile(
65-
formData: FormData,
66-
file: any
67-
): FormData | Error {
74+
function EncodeMultipartFormDataFile(formData: FormData, file: any): FormData {
6875
if (typeof file !== "object" || Array.isArray(file) || file == null) {
69-
return new Error("invalid type for multipart/form-data file");
76+
throw new Error("invalid type for multipart/form-data file");
7077
}
7178
const fieldNames: string[] = Object.getOwnPropertyNames(file);
72-
const content: string | undefined = undefined;
73-
let fileName: string | undefined = undefined;
79+
let content: any = null;
80+
let fileName = "";
81+
let mpFormDecoratorName = "";
82+
7483
fieldNames.forEach((fname) => {
7584
const mpFormAnn: string = Reflect.getMetadata(
7685
mpFormMetadataKey,
@@ -80,15 +89,23 @@ function EncodeMultipartFormDataFile(
8089
if (mpFormAnn == null) return;
8190
const mpFormDecorator: MultipartFormDecorator =
8291
ParseMultipartFormDecorator(mpFormAnn);
83-
let content = "";
8492
if (!mpFormDecorator.Content && mpFormDecorator.Name == "") return;
8593
if (mpFormDecorator.Content) content = file[fname];
86-
else fileName = file[fname];
94+
else {
95+
mpFormDecoratorName = mpFormDecorator.Name;
96+
fileName = file[fname];
97+
}
8798
});
8899

89-
if (content == null || fileName == null)
90-
return new Error("invalid multipart/form-data file");
91-
formData.append(fileName!, content!);
100+
if (mpFormDecoratorName === "" || fileName === "" || content == null)
101+
throw new Error("invalid multipart/form-data file");
102+
writeFileSync(join(__dirname, fileName), content, {
103+
flag: "w",
104+
});
105+
formData.append(
106+
mpFormDecoratorName,
107+
createReadStream(join(__dirname, fileName))
108+
);
92109
return formData;
93110
}
94111

src/sdk/models/operations/deleteapi.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,19 @@ export class DeleteApiPathParams {
66
ApiId: string;
77
@Metadata("pathParam, style=simple;explode=false;name=versionID")
88
VersionId: string;
9+
10+
constructor(ApiId: string, VersionId: string) {
11+
this.ApiId = ApiId;
12+
this.VersionId = VersionId;
13+
}
914
}
1015

1116
export class DeleteApiRequest {
1217
PathParams: DeleteApiPathParams;
18+
19+
constructor(PathParams: DeleteApiPathParams) {
20+
this.PathParams = PathParams;
21+
}
1322
}
1423

1524
export class DeleteApiResponse {
@@ -18,4 +27,10 @@ export class DeleteApiResponse {
1827
Error?: shared.Error;
1928

2029
StatusCode: number;
30+
31+
constructor(ContentType: string, StatusCode: number, Error?: shared.Error) {
32+
this.ContentType = ContentType;
33+
this.Error = Error;
34+
this.StatusCode = StatusCode;
35+
}
2136
}

src/sdk/models/operations/deleteapiendpoint.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@ export class DeleteApiEndpointPathParams {
88
ApiId: string;
99
@Metadata("pathParam, style=simple;explode=false;name=versionID")
1010
VersionId: string;
11+
12+
constructor(ApiEndpointId: string, ApiId: string, VersionId: string) {
13+
this.ApiEndpointId = ApiEndpointId;
14+
this.ApiId = ApiId;
15+
this.VersionId = VersionId;
16+
}
1117
}
1218

1319
export class DeleteApiEndpointRequest {
1420
PathParams: DeleteApiEndpointPathParams;
21+
22+
constructor(PathParams: DeleteApiEndpointPathParams) {
23+
this.PathParams = PathParams;
24+
}
1525
}
1626

1727
export class DeleteApiEndpointResponse {
@@ -20,4 +30,10 @@ export class DeleteApiEndpointResponse {
2030
Error?: shared.Error;
2131

2232
StatusCode: number;
33+
34+
constructor(ContentType: string, StatusCode: number, Error?: shared.Error) {
35+
this.ContentType = ContentType;
36+
this.Error = Error;
37+
this.StatusCode = StatusCode;
38+
}
2339
}

src/sdk/models/operations/deleteschema.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@ export class DeleteSchemaPathParams {
88
RevisionId: string;
99
@Metadata("pathParam, style=simple;explode=false;name=versionID")
1010
VersionId: string;
11+
12+
constructor(ApiId: string, RevisionId: string, VersionId: string) {
13+
this.ApiId = ApiId;
14+
this.RevisionId = RevisionId;
15+
this.VersionId = VersionId;
16+
}
1117
}
1218

1319
export class DeleteSchemaRequest {
1420
PathParams: DeleteSchemaPathParams;
21+
22+
constructor(PathParams: DeleteSchemaPathParams) {
23+
this.PathParams = PathParams;
24+
}
1525
}
1626

1727
export class DeleteSchemaResponse {
@@ -20,4 +30,10 @@ export class DeleteSchemaResponse {
2030
Error?: shared.Error;
2131

2232
StatusCode: number;
33+
34+
constructor(ContentType: string, StatusCode: number, Error?: shared.Error) {
35+
this.ContentType = ContentType;
36+
this.Error = Error;
37+
this.StatusCode = StatusCode;
38+
}
2339
}

src/sdk/models/operations/deleteversionmetadata.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,26 @@ export class DeleteVersionMetadataPathParams {
1010
MetaValue: string;
1111
@Metadata("pathParam, style=simple;explode=false;name=versionID")
1212
VersionId: string;
13+
14+
constructor(
15+
ApiId: string,
16+
MetaKey: string,
17+
MetaValue: string,
18+
VersionId: string
19+
) {
20+
this.ApiId = ApiId;
21+
this.MetaKey = MetaKey;
22+
this.MetaValue = MetaValue;
23+
this.VersionId = VersionId;
24+
}
1325
}
1426

1527
export class DeleteVersionMetadataRequest {
1628
PathParams: DeleteVersionMetadataPathParams;
29+
30+
constructor(PathParams: DeleteVersionMetadataPathParams) {
31+
this.PathParams = PathParams;
32+
}
1733
}
1834

1935
export class DeleteVersionMetadataResponse {
@@ -22,4 +38,10 @@ export class DeleteVersionMetadataResponse {
2238
Error?: shared.Error;
2339

2440
StatusCode: number;
41+
42+
constructor(ContentType: string, StatusCode: number, Error?: shared.Error) {
43+
this.ContentType = ContentType;
44+
this.Error = Error;
45+
this.StatusCode = StatusCode;
46+
}
2547
}

src/sdk/models/operations/downloadschema.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,19 @@ export class DownloadSchemaPathParams {
66
ApiId: string;
77
@Metadata("pathParam, style=simple;explode=false;name=versionID")
88
VersionId: string;
9+
10+
constructor(ApiId: string, VersionId: string) {
11+
this.ApiId = ApiId;
12+
this.VersionId = VersionId;
13+
}
914
}
1015

1116
export class DownloadSchemaRequest {
1217
PathParams: DownloadSchemaPathParams;
18+
19+
constructor(PathParams: DownloadSchemaPathParams) {
20+
this.PathParams = PathParams;
21+
}
1322
}
1423

1524
export class DownloadSchemaResponse {
@@ -20,4 +29,16 @@ export class DownloadSchemaResponse {
2029
Schema?: string;
2130

2231
StatusCode: number;
32+
33+
constructor(
34+
ContentType: string,
35+
StatusCode: number,
36+
Error?: shared.Error,
37+
Schema?: string
38+
) {
39+
this.ContentType = ContentType;
40+
this.Error = Error;
41+
this.Schema = Schema;
42+
this.StatusCode = StatusCode;
43+
}
2344
}

0 commit comments

Comments
 (0)