Skip to content

Commit

Permalink
fix(postman): upgrade from ir v23 to ir v53 (#4378)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi committed Aug 22, 2024
1 parent 631355e commit 0d2df13
Show file tree
Hide file tree
Showing 99 changed files with 11,659 additions and 980 deletions.
1 change: 1 addition & 0 deletions .github/workflows/lint-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
go
ruby
seed
postman
requireScope: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4 changes: 4 additions & 0 deletions generators/postman/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.0] - 2024-08-21

- Internal: Upgrade the Postman generator to use IR version 53.

## [0.1.1] - 2024-03-22

- Internal: Shared generator notification and config parsing logic.
Expand Down
2 changes: 1 addition & 1 deletion generators/postman/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.1
0.2.0
3 changes: 2 additions & 1 deletion generators/postman/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
"dockerTagVersion": "pnpm dist:cli && docker build -f ./Dockerfile -t fernapi/fern-postman:${0} ."
},
"dependencies": {
"@fern-api/core-utils": "workspace:*",
"@fern-api/generator-commons": "workspace:*",
"@fern-fern/ir-sdk": "0.0.2828",
"@fern-fern/ir-sdk": "^53.7.0",
"@fern-fern/postman-sdk": "0.0.46",
"@types/lodash": "^4.17.4",
"endent": "^2.1.0",
Expand Down
22 changes: 20 additions & 2 deletions generators/postman/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ export function convertAuth(schemes: AuthScheme[]): PostmanRequestAuth | undefin
}
]
}),
oauth: () => ({
type: "bearer",
bearer: [
{
key: "token",
value: getReferenceToVariable(BEARER_AUTH_TOKEN_VARIABLE),
type: "string"
}
]
}),
header: (header) => {
return {
type: "apikey",
Expand Down Expand Up @@ -71,10 +81,11 @@ export function convertAuth(schemes: AuthScheme[]): PostmanRequestAuth | undefin

export function getAuthHeaders(schemes: AuthScheme[]): PostmanHeader[] {
return schemes.flatMap((scheme) =>
AuthScheme._visit(scheme, {
AuthScheme._visit<PostmanHeader[]>(scheme, {
basic: () => [],
bearer: () => [],
header: (header) => [
oauth: () => [],
header: (header): PostmanHeader[] => [
{
key: header.name.wireValue,
value: getReferenceToVariable(getVariableForAuthHeader(header)),
Expand Down Expand Up @@ -110,6 +121,13 @@ export function getVariablesForAuthScheme(scheme: AuthScheme): PostmanVariable[]
type: "string"
}
],
oauth: () => [
{
key: BEARER_AUTH_TOKEN_VARIABLE,
value: "",
type: "string"
}
],
header: (header) => [
{
key: getVariableForAuthHeader(header),
Expand Down
37 changes: 31 additions & 6 deletions generators/postman/src/convertExampleEndpointCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TypeDeclaration
} from "@fern-fern/ir-sdk/api";
import { PostmanExampleResponse, PostmanHeader } from "@fern-fern/postman-sdk/api";
import { isEqual, startCase } from "lodash";
import { isEqual, startCase, values } from "lodash";
import { GeneratedExampleRequest } from "./request/GeneratedExampleRequest";

export function convertExampleEndpointCall({
Expand Down Expand Up @@ -35,15 +35,40 @@ export function convertExampleEndpointCall({
return {
...getNameAndStatus({ example, allErrors }),
originalRequest: generatedRequest,
description: httpEndpoint.response?.docs ?? undefined,
body:
example.response.body?.jsonExample != null
? JSON.stringify(example.response.body.jsonExample, undefined, 4)
: "",
description:
httpEndpoint.response?.body?._visit({
fileDownload: (value) => value.docs,
json: (value) => value.docs,
streamParameter: (value) => value.streamResponse.docs,
streaming: (value) => value.docs,
text: (value) => value.docs,
_other: () => undefined
}) ?? undefined,
body: example.response._visit({
ok: (value) => {
return value._visit({
body: (value) => maybeStringify({ jsonExample: value?.jsonExample }),
sse: (value) => maybeStringify({ jsonExample: value.map((event) => event?.data.jsonExample) }),
stream: (value) => maybeStringify({ jsonExample: value.map((event) => event.jsonExample) }),
_other: () => ""
});
},
error: (value) => {
return maybeStringify({ jsonExample: value?.body?.jsonExample });
},
_other: () => ""
}),
postmanPreviewlanguage: "json"
};
}

function maybeStringify({ jsonExample }: { jsonExample?: unknown }): string {
if (jsonExample == null) {
return "";
}
return JSON.stringify(jsonExample, undefined, 4);
}

function getNameAndStatus({
example,
allErrors
Expand Down
56 changes: 39 additions & 17 deletions generators/postman/src/convertToPostmanCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import {
PostmanHeader
} from "@fern-fern/postman-sdk/api";
import { startCase } from "lodash";
import { isNonNullish } from "@fern-api/core-utils";
import { convertAuth, getAuthHeaders, getVariablesForAuthScheme } from "./auth";
import { convertExampleEndpointCall } from "./convertExampleEndpointCall";
import { GeneratedDummyRequest } from "./request/GeneratedDummyRequest";
import { GeneratedExampleRequest } from "./request/GeneratedExampleRequest";
import { ORIGIN_VARIABLE_NAME } from "./utils";

Expand Down Expand Up @@ -82,6 +82,12 @@ function filterAuthSchemes(auth: ApiAuth): AuthScheme[] {
}
return (hasSeenAuthorizationHeader = true);
},
oauth: () => {
if (hasSeenAuthorizationHeader) {
return false;
}
return (hasSeenAuthorizationHeader = true);
},
header: () => true,
_other: () => {
throw new Error("Unknown auth scheme: " + scheme.type);
Expand Down Expand Up @@ -129,18 +135,24 @@ function getCollectionItemsForPackage(
throw new Error("Service does not exist: " + package_.service);
}
items.push(
...service.endpoints.map(
(httpEndpoint): PostmanCollectionItem.Endpoint => ({
type: "endpoint",
...convertEndpoint({
...service.endpoints
.map((httpEndpoint): PostmanCollectionItem.Endpoint | undefined => {
const convertedEndpoint = convertEndpoint({
authHeaders,
httpEndpoint,
httpService: service,
allTypes: Object.values(ir.types),
allErrors: Object.values(ir.errors)
})
});
if (convertedEndpoint != null) {
return {
type: "endpoint",
...convertedEndpoint
};
}
return undefined;
})
)
.filter(isNonNullish)
);
}

Expand All @@ -159,18 +171,28 @@ function convertEndpoint({
httpService: HttpService;
allTypes: TypeDeclaration[];
allErrors: ErrorDeclaration[];
}): PostmanCollectionEndpointItem {
const example = httpEndpoint.examples[0];
const generatedRequest =
example != null
? new GeneratedExampleRequest({ authHeaders, httpService, httpEndpoint, allTypes, example })
: new GeneratedDummyRequest({ authHeaders, httpService, httpEndpoint, allTypes });
}): PostmanCollectionEndpointItem | undefined {
const example = httpEndpoint.userSpecifiedExamples[0]?.example ?? httpEndpoint.autogeneratedExamples[0]?.example;

if (example == null) {
return undefined;
}

return {
name: httpEndpoint.displayName ?? startCase(httpEndpoint.name.originalName),
request: generatedRequest.get(),
response: httpEndpoint.examples.map((example) =>
convertExampleEndpointCall({ authHeaders, httpService, httpEndpoint, allTypes, allErrors, example })
)
request: new GeneratedExampleRequest({ authHeaders, httpService, httpEndpoint, allTypes, example }).get(),
response: [...httpEndpoint.userSpecifiedExamples, ...httpEndpoint.autogeneratedExamples]
.map((example) => example.example)
.filter(isNonNullish)
.map((example) =>
convertExampleEndpointCall({
authHeaders,
httpService,
httpEndpoint,
allTypes,
allErrors,
example
})
)
};
}
Loading

0 comments on commit 0d2df13

Please sign in to comment.