Skip to content

Commit

Permalink
feat(cli): support bytes in the fern definition
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi committed Dec 11, 2024
1 parent 7687e8b commit ef8c601
Show file tree
Hide file tree
Showing 49 changed files with 2,851 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import {
NonStreamHttpResponseBody,
StreamingResponse
} from "@fern-api/ir-sdk";
import { isRawTextType, parseRawFileType, parseRawTextType, RawSchemas } from "@fern-api/fern-definition-schema";
import {
isRawTextType,
parseRawFileType,
parseRawTextType,
parseRawBytesType,
RawSchemas
} from "@fern-api/fern-definition-schema";
import { FernFileContext } from "../../FernFileContext";
import { TypeResolver } from "../../resolvers/TypeResolver";
import { getObjectPropertyFromResolvedType } from "./getObjectPropertyFromResolvedType";
Expand Down Expand Up @@ -69,6 +75,10 @@ export function convertHttpResponseBody({
nonStreamResponse = NonStreamHttpResponseBody.text({ ...response });
break;
}
case "bytes": {
nonStreamResponse = NonStreamHttpResponseBody.bytes({ ...response });
break;
}
default:
assertNever(response);
}
Expand All @@ -93,7 +103,7 @@ export function convertNonStreamHttpResponseBody({
endpoint: RawSchemas.HttpEndpointSchema;
file: FernFileContext;
typeResolver: TypeResolver;
}): HttpResponseBody.FileDownload | HttpResponseBody.Text | HttpResponseBody.Json | undefined {
}): HttpResponseBody.FileDownload | HttpResponseBody.Text | HttpResponseBody.Json | HttpResponseBody.Bytes | undefined {
const { response } = endpoint;

if (response != null) {
Expand All @@ -108,6 +118,10 @@ export function convertNonStreamHttpResponseBody({
return HttpResponseBody.text({
docs
});
} else if (parseRawBytesType(responseType) != null) {
return HttpResponseBody.bytes({
docs
});
} else {
return convertJsonResponse(response, docs, file, typeResolver);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ export function generateEndpointExample({
return { type: "failure", message: "Streaming unsupported" };
case "text":
return { type: "failure", message: "Text unsupported" };
case "bytes":
return { type: "failure", message: "Bytes unsupported" };
default:
assertNever(endpoint.response.body);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ export class IrGraph {
}
},
text: noop,
bytes: noop,
_other: () => {
throw new Error("Unknown HttpResponse: " + httpEndpoint.response?.body?.type);
}
Expand Down
1 change: 1 addition & 0 deletions packages/cli/generation/ir-migrations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"@fern-fern/ir-v50-sdk": "0.0.3",
"@fern-fern/ir-v51-sdk": "0.0.1",
"@fern-fern/ir-v52-sdk": "0.0.1",
"@fern-fern/ir-v53-sdk": "0.0.1",
"@fern-fern/ir-v6-model": "0.0.33",
"@fern-fern/ir-v7-model": "0.0.2",
"@fern-fern/ir-v8-model": "0.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { V50_TO_V49_MIGRATION } from "./migrations/v50-to-v49/migrateFromV50ToV4
import { V51_TO_V50_MIGRATION } from "./migrations/v51-to-v50/migrateFromV51ToV50";
import { V52_TO_V51_MIGRATION } from "./migrations/v52-to-v51/migrateFromV52ToV51";
import { V53_TO_V52_MIGRATION } from "./migrations/v53-to-v52/migrateFromV53ToV52";
import { V54_TO_V53_MIGRATION } from "./migrations/v54-to-v53/migrateFromV54ToV53";
import { V6_TO_V5_MIGRATION } from "./migrations/v6-to-v5/migrateFromV6ToV5";
import { V7_TO_V6_MIGRATION } from "./migrations/v7-to-v6/migrateFromV7ToV6";
import { V8_TO_V7_MIGRATION } from "./migrations/v8-to-v7/migrateFromV8ToV7";
Expand Down Expand Up @@ -279,6 +280,7 @@ const IntermediateRepresentationMigrator = {

const INTERMEDIATE_REPRESENTATION_MIGRATOR = IntermediateRepresentationMigrator.Builder
// put new migrations here
.withMigration(V54_TO_V53_MIGRATION)
.withMigration(V53_TO_V52_MIGRATION)
.withMigration(V52_TO_V51_MIGRATION)
.withMigration(V51_TO_V50_MIGRATION)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { serialization as V53 } from "@fern-api/ir-sdk";
export { serialization as V54 } from "@fern-api/ir-sdk";
export * as V23 from "@fern-fern/ir-v23-sdk/serialization";
export * as V24 from "@fern-fern/ir-v24-sdk/serialization";
export * as V25 from "@fern-fern/ir-v25-sdk/serialization";
Expand Down Expand Up @@ -29,3 +29,4 @@ export * as V49 from "@fern-fern/ir-v49-sdk/serialization";
export * as V50 from "@fern-fern/ir-v50-sdk/serialization";
export * as V51 from "@fern-fern/ir-v51-sdk/serialization";
export * as V52 from "@fern-fern/ir-v52-sdk/serialization";
export * as V53 from "@fern-fern/ir-v53-sdk/serialization";
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { FernIr as V53 } from "@fern-api/ir-sdk";
export { FernIr as V54 } from "@fern-api/ir-sdk";
export * as V1 from "@fern-fern/ir-v1-model";
export * as V10 from "@fern-fern/ir-v10-model";
export * as V11 from "@fern-fern/ir-v11-model";
Expand Down Expand Up @@ -47,6 +47,7 @@ export * as V5 from "@fern-fern/ir-v5-model";
export { FernIrV50 as V50 } from "@fern-fern/ir-v50-sdk";
export { FernIrV51 as V51 } from "@fern-fern/ir-v51-sdk";
export { FernIrV52 as V52 } from "@fern-fern/ir-v52-sdk";
export { FernIrV53 as V53 } from "@fern-fern/ir-v53-sdk";
export * as V6 from "@fern-fern/ir-v6-model";
export * as V7 from "@fern-fern/ir-v7-model";
export * as V8 from "@fern-fern/ir-v8-model";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: simple-api
auth: bearer
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
types:
AudioFormat:
enum:
- MP3
- WAV
- OGG

service:
auth: false
base-path: /
endpoints:
getAudioFile:
method: GET
path: /audio
request:
name: GetAudioRequest
query-parameters:
format:
type: AudioFormat
default: MP3
response: bytes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { AbsoluteFilePath, join, RelativeFilePath } from "@fern-api/fs-utils";
import { createMigrationTester } from "../../../__test__/utils/createMigrationTester";
import { V54_TO_V53_MIGRATION } from "../migrateFromV54ToV53";

const runMigration = createMigrationTester(V54_TO_V53_MIGRATION);

describe("migrateFromV54ToV53", () => {
it("simple", async () => {
const pathToFixture = join(AbsoluteFilePath.of(__dirname), RelativeFilePath.of("./fixtures/simple"));
const migrated = await runMigration({
pathToFixture
});
expect(await migrated.jsonify()).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { GeneratorName } from "@fern-api/configuration-loader";
import { assertNever } from "@fern-api/core-utils";
import { mapValues } from "lodash-es";
import { IrSerialization } from "../../ir-serialization";
import { IrVersions } from "../../ir-versions";
import {
GeneratorWasNeverUpdatedToConsumeNewIR,
GeneratorWasNotCreatedYet,
IrMigration
} from "../../types/IrMigration";

export const V54_TO_V53_MIGRATION: IrMigration<
IrVersions.V54.ir.IntermediateRepresentation,
IrVersions.V53.ir.IntermediateRepresentation
> = {
laterVersion: "v54",
earlierVersion: "v53",
firstGeneratorVersionToConsumeNewIR: {
[GeneratorName.TYPESCRIPT_NODE_SDK]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.TYPESCRIPT_BROWSER_SDK]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.TYPESCRIPT]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.TYPESCRIPT_SDK]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.TYPESCRIPT_EXPRESS]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.JAVA]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.JAVA_MODEL]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.JAVA_SDK]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.JAVA_SPRING]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.PYTHON_FASTAPI]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.PYTHON_PYDANTIC]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.OPENAPI_PYTHON_CLIENT]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.OPENAPI]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.STOPLIGHT]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.POSTMAN]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.PYTHON_SDK]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.GO_FIBER]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.GO_MODEL]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.GO_SDK]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.RUBY_MODEL]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.RUBY_SDK]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.CSHARP_MODEL]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.CSHARP_SDK]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.SWIFT_MODEL]: GeneratorWasNeverUpdatedToConsumeNewIR,
[GeneratorName.SWIFT_SDK]: GeneratorWasNotCreatedYet,
[GeneratorName.PHP_MODEL]: GeneratorWasNotCreatedYet,
[GeneratorName.PHP_SDK]: GeneratorWasNeverUpdatedToConsumeNewIR
},
jsonifyEarlierVersion: (ir) =>
IrSerialization.V53.IntermediateRepresentation.jsonOrThrow(ir, {
unrecognizedObjectKeys: "strip",
skipValidation: true
}),
migrateBackwards: (v54): IrVersions.V53.ir.IntermediateRepresentation => {
return {
...v54,
services: mapValues(v54.services, (service) => ({
...service,
endpoints: service.endpoints.map((endpoint) => ({
...endpoint,
response: convertHttpResponse(endpoint.response)
}))
}))
};
}
};

function convertHttpResponse(
response: IrVersions.V54.HttpResponse | undefined
): IrVersions.V53.HttpResponse | undefined {
if (response == null) {
return undefined;
}

return {
statusCode: response.statusCode,
body:
response.body?.type === "bytes"
? IrVersions.V53.HttpResponseBody.json(
IrVersions.V53.JsonResponse.response({
docs: undefined,
responseBodyType: IrVersions.V53.TypeReference.unknown()
})
)
: (response.body as any)

Check warning on line 83 in packages/cli/generation/ir-migrations/src/migrations/v54-to-v53/migrateFromV54ToV53.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ function convertResponse(irResponse: Ir.http.HttpResponse): FdrCjsSdk.api.v1.reg
};
},
text: () => undefined, // TODO: support text/plain in FDR
bytes: () => undefined, // TODO: support text/plain in FDR
streamParameter: () => undefined, // TODO: support stream parameter in FDR
streaming: (streamingResponse) => {
if (streamingResponse.type === "text") {
Expand Down
2 changes: 1 addition & 1 deletion packages/ir-sdk/fern/apis/ir-types-latest/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
53.24.0
54.0.0
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ 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).

## [v54.0.0] - 2024-12-11

- Break: The HttpResponse type in the IR now supports bytes responses. This is useful for different languages -
for example TypeScript can return an `ArrayBuffer` instead of `stream.Readable` in this case.

## [v53.24.0] - 2024-11-04

- Feature: The dynamic snippets IR supports a configurable baseURL and/or environment.
Expand Down
5 changes: 5 additions & 0 deletions packages/ir-sdk/fern/apis/ir-types-latest/definition/http.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,14 @@ types:
json: JsonResponse
fileDownload: FileDownloadResponse
text: TextResponse
bytes: BytesResponse

HttpResponseBody:
union:
json: JsonResponse
fileDownload: FileDownloadResponse
text: TextResponse
bytes: BytesResponse
streaming: StreamingResponse
streamParameter:
type: StreamParameterResponse
Expand Down Expand Up @@ -235,6 +237,9 @@ types:

TextResponse:
extends: commons.WithDocs

BytesResponse:
extends: commons.WithDocs

StreamParameterResponse:
properties:
Expand Down
1 change: 1 addition & 0 deletions packages/ir-sdk/fern/apis/ir-types-v53/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
53.23.0
Loading

0 comments on commit ef8c601

Please sign in to comment.