Skip to content

Commit

Permalink
Merge branch 'niels/ts/inline-types' of https://github.com/fern-api/fern
Browse files Browse the repository at this point in the history
 into niels/ts/inline-types
  • Loading branch information
Swimburger committed Dec 11, 2024
2 parents 8bb8e7c + 970d79d commit 76eb20c
Show file tree
Hide file tree
Showing 53 changed files with 3,923 additions and 9 deletions.
4 changes: 4 additions & 0 deletions fern/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ navigation:
icon: fa-regular fa-file
path: ./pages/api-definition/fern-definition/endpoints/multipart.mdx
slug: multipart
- page: Bytes
path: ./pages/api-definition/fern-definition/endpoints/bytes.mdx
icon: fa-regular fa-server
slug: bytes
- page: Server Sent Events
icon: fa-regular fa-signal-stream
path: ./pages/api-definition/fern-definition/endpoints/sse.mdx
Expand Down
55 changes: 55 additions & 0 deletions fern/pages/api-definition/fern-definition/endpoints/bytes.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
title: Binary Data and Files
subtitle: Use the `bytes` type to handle binary data in your API
---

<Note>
The `bytes` type allows you to handle binary data in both requests and responses.
</Note>

## Sending bytes

If your API needs to send a stream of bytes (i.e. typical for assets like audio, images and other files) then
you can use the `bytes` type in the Fern Definition to model this.

```yml audio.yml
service:
base-path: /audio
endpoints:
upload:
display-name: Upload audio
method: POST
path: /upload
content-type: application/octet-stream
request:
type: bytes
docs: The bytes of the MP3 file that you would like to upload
```
## Receiving bytes
On the other hand, if your API is returning a stream of bytes, then you can leverage the `bytes` type as a response.

```yml textToSpeech.yml
service:
base-path: /tts
endpoints:
upload:
display-name: Upload audio
method: POST
path: ""
request:
name: TTSRequest
body:
properties:
text:
type: string
docs: The text that you want converted to speach.
response:
type: bytes
docs: The bytes of the audio file.
```




17 changes: 15 additions & 2 deletions packages/cli/cli/versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,21 @@
* Set `inline: true` for maps generated from OpenAPI additionalProperties.
type: fix
irVersion: 53
version: 0.45.5

version: 0.46.0-rc1

- changelogEntry:
- summary: |
The Fern Definition now supports `bytes` as a response type.
```yml service.yml
endpoints:
download:
response: bytes
```
type: feat
irVersion: 53
version: 0.46.0-rc0

- changelogEntry:
- summary: |
Defaults are no longer set on datetimes when converting to docs shapes.
Expand Down
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
Loading

0 comments on commit 76eb20c

Please sign in to comment.