Skip to content

Commit

Permalink
fix(cli): Remove crypto module from @fern-api/ir-generator (#5358)
Browse files Browse the repository at this point in the history
  • Loading branch information
amckinney authored Dec 6, 2024
1 parent cfb9862 commit 046c466
Show file tree
Hide file tree
Showing 47 changed files with 202 additions and 176 deletions.
11 changes: 5 additions & 6 deletions packages/cli/api-importers/commons/src/FernDefinitionBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { FERN_PACKAGE_MARKER_FILENAME, ROOT_API_FILENAME } from "@fern-api/configuration";
import { AbsoluteFilePath, dirname, relative, RelativeFilePath } from "@fern-api/path-utils";
import { AbsoluteFilePath, basename, dirname, join, relative, RelativeFilePath } from "@fern-api/path-utils";
import { RawSchemas, RootApiFileSchema, visitRawEnvironmentDeclaration } from "@fern-api/fern-definition-schema";
import { camelCase, isEqual } from "lodash-es";
import path, { basename, extname } from "path";
import { FernDefinitionDirectory } from "./utils/FernDefinitionDirectory";

export type HttpServiceInfo = Partial<
Expand Down Expand Up @@ -260,7 +259,7 @@ export class FernDefinitionBuilderImpl implements FernDefinitionBuilder {
const importPrefix =
alias ??
camelCase(
(dirname(fileToImport) + "/" + basename(fileToImport, extname(fileToImport))).replaceAll(
(dirname(fileToImport) + "/" + basename(fileToImport, { stripExtension: true })).replaceAll(
"__package__",
"root"
)
Expand Down Expand Up @@ -401,7 +400,7 @@ export class FernDefinitionBuilderImpl implements FernDefinitionBuilder {

const basePath = this.basePath;
if (basePath != null) {
fernFile.channel.path = path.join(basePath, channel.path);
fernFile.channel.path = join(basePath, channel.path);
}
}

Expand Down Expand Up @@ -456,7 +455,7 @@ export class FernDefinitionBuilderImpl implements FernDefinitionBuilder {
id,
{
...endpoint,
path: path.join(basePath, endpoint.path)
path: join(basePath, endpoint.path)
}
];
})
Expand All @@ -475,7 +474,7 @@ export class FernDefinitionBuilderImpl implements FernDefinitionBuilder {
id,
{
...endpoint,
path: path.join(basePath, endpoint.path)
path: join(basePath, endpoint.path)
}
];
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
}
}
}
},
"components": {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
}
}
}
},
"components": {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@
}
}
}
},
"components": {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
}
}
}
},
"components": {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
}
],
"x-fern-base-path": "/v1",
"paths": {},
"components": {}
"paths": {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
}
}
}
},
"components": {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
"#/paths/~1users/get/responses/200/content/application~1json/schema/items"
]
}
],
"components": {}
]
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@
"autogeneratedExamples": [
{
"example": {
"id": "aa85be3225bcafa9a33883eb24ae6cd211e94e87",
"id": "f33d5ef1",
"url": "/movies/create-movie",
"name": null,
"endpointHeaders": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@
"autogeneratedExamples": [
{
"example": {
"id": "7e89e29030a150804050f857160171c6a27118e9",
"id": "b80ff823",
"url": "/test-availability",
"name": null,
"endpointHeaders": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@
"autogeneratedExamples": [
{
"example": {
"id": "ac0ed44663a8a1cf969bb5ddff0a2a3e6e61ba14",
"id": "1324396e",
"url": "/test-description",
"name": null,
"endpointHeaders": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1660,7 +1660,7 @@
"autogeneratedExamples": [
{
"example": {
"id": "aa85be3225bcafa9a33883eb24ae6cd211e94e87",
"id": "f33d5ef1",
"url": "/movies/create-movie",
"name": null,
"endpointHeaders": [],
Expand Down Expand Up @@ -2438,7 +2438,7 @@
"userSpecifiedExamples": [
{
"example": {
"id": "1a0bea08b783bd21407a830ddbfc675ca14b3769b65ad0aefbd97a3b2da36772",
"id": "96e61367",
"name": null,
"url": "/movies/tt0111161",
"rootPathParameters": [],
Expand Down Expand Up @@ -3023,7 +3023,7 @@
},
{
"example": {
"id": "f3eabd4e7bc34c1e9b2ea99ffbf275d00fe11c2f7a8202309767ad09d0b292db",
"id": "712d57e",
"name": null,
"url": "/movies/tt1234",
"rootPathParameters": [],
Expand Down Expand Up @@ -3303,7 +3303,7 @@
"autogeneratedExamples": [
{
"example": {
"id": "6bef1c7345345a27ad8e0b27a87975e4a77ea286",
"id": "bc908069",
"url": "/movies/id",
"name": null,
"endpointHeaders": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@
"autogeneratedExamples": [
{
"example": {
"id": "aa85be3225bcafa9a33883eb24ae6cd211e94e87",
"id": "f33d5ef1",
"url": "/movies/create-movie",
"name": null,
"endpointHeaders": [],
Expand Down Expand Up @@ -1969,7 +1969,7 @@
"userSpecifiedExamples": [
{
"example": {
"id": "1a0bea08b783bd21407a830ddbfc675ca14b3769b65ad0aefbd97a3b2da36772",
"id": "96e61367",
"name": null,
"url": "/movies/tt0111161",
"rootPathParameters": [],
Expand Down Expand Up @@ -2554,7 +2554,7 @@
},
{
"example": {
"id": "f3eabd4e7bc34c1e9b2ea99ffbf275d00fe11c2f7a8202309767ad09d0b292db",
"id": "712d57e",
"name": null,
"url": "/movies/tt1234",
"rootPathParameters": [],
Expand Down Expand Up @@ -2834,7 +2834,7 @@
"autogeneratedExamples": [
{
"example": {
"id": "d9df8c5dbf4b0fd3b0b3b8931c9c97aec5bbb006",
"id": "4c14f8e8",
"url": "/movies/id",
"name": null,
"endpointHeaders": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@
"autogeneratedExamples": [
{
"example": {
"id": "aa85be3225bcafa9a33883eb24ae6cd211e94e87",
"id": "f33d5ef1",
"url": "/movies/create-movie",
"name": null,
"endpointHeaders": [],
Expand Down
44 changes: 44 additions & 0 deletions packages/cli/generation/ir-generator/build.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const packageJson = require("./package.json");
const tsup = require('tsup');
const { writeFile, rename } = require("fs/promises");
const path = require("path");

main();

async function main() {
await tsup.build({
entry: ['src/**/*.ts', '!src/__test__'],
format: ['cjs'],
clean: true,
// minify: true,
dts: true,
outDir: 'dist',
target: "es2017",
external: [
// Exclude the optional dependencies that aren't supported in the browser.
'prettier',
],
tsconfig: "./build.tsconfig.json"
});

process.chdir(path.join(__dirname, "dist"));

// The module expects the imports defined in the index.d.ts file.
await rename("index.d.cts", "index.d.ts");

await writeFile(
"package.json",
JSON.stringify(
{
name: packageJson.name,
version: process.argv[2] || packageJson.version,
repository: packageJson.repository,
main: "index.cjs",
types: "index.d.ts",
files: ["index.cjs", "index.d.ts"]
},
undefined,
2
)
);
}
5 changes: 5 additions & 0 deletions packages/cli/generation/ir-generator/build.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "../../../../shared/tsconfig.shared.json",
"include": ["src/**/*.ts"],
"exclude": ["src/**/__test__"]
}
17 changes: 8 additions & 9 deletions packages/cli/generation/ir-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
"format": "prettier --write --ignore-unknown --ignore-path ../../../../shared/.prettierignore \"**\"",
"format:check": "prettier --check --ignore-unknown --ignore-path ../../../../shared/.prettierignore \"**\"",
"organize-imports": "organize-imports-cli tsconfig.json",
"depcheck": "depcheck"
"depcheck": "depcheck",
"dist": "pnpm compile && node build.cjs"
},
"dependencies": {
"devDependencies": {
"@fern-api/api-workspace-commons": "workspace:*",
"@fern-api/configuration": "workspace:*",
"@fern-api/core-utils": "workspace:*",
Expand All @@ -36,20 +37,18 @@
"@fern-api/ir-sdk": "workspace:*",
"@fern-api/source-resolver": "workspace:*",
"@fern-api/task-context": "workspace:*",
"lodash-es": "^4.17.21",
"object-hash": "^3.0.0",
"prettier": "^2.7.1",
"url-join": "^5.0.0"
},
"devDependencies": {
"@types/lodash-es": "^4.17.12",
"@types/object-hash": "^3.0.6",
"@types/prettier": "^2.7.1",
"@types/url-join": "4.0.1",
"depcheck": "^1.4.6",
"eslint": "^8.56.0",
"lodash-es": "^4.17.21",
"organize-imports-cli": "^0.10.0",
"prettier": "^2.7.1",
"string.prototype.replaceall": "^1.0.10",
"tsup": "^8.3.5",
"typescript": "4.6.4",
"url-join": "^5.0.0",
"vitest": "^2.0.5"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
RawSchemas,
visitExampleResponseSchema
} from "@fern-api/fern-definition-schema";
import crypto from "crypto";
import { FernFileContext } from "../../FernFileContext";
import { ErrorResolver } from "../../resolvers/ErrorResolver";
import { ExampleResolver } from "../../resolvers/ExampleResolver";
Expand All @@ -34,11 +33,7 @@ import { getHeaderName, resolvePathParameterOrThrow } from "./convertHttpService
import { getQueryParameterName } from "./convertQueryParameter";
import urlJoin from "url-join";
import { getEndpointPathParameters } from "../../utils/getEndpointPathParameters";

function hashJSON(obj: unknown): string {
const jsonString = JSON.stringify(obj);
return crypto.createHash("sha256").update(jsonString).digest("hex");
}
import { hashJSON } from "../../utils/hashJSON";

export function convertExampleEndpointCall({
service,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { ExampleGenerationResult } from "./ExampleGenerationResult";
import { generateTypeReferenceExample } from "./generateTypeReferenceExample";
import { isOptional } from "./isTypeReferenceOptional";
import hash from "object-hash";
import { hashJSON } from "../../utils/hashJSON";

export declare namespace generateEndpointExample {
interface Args {
Expand Down Expand Up @@ -293,7 +293,7 @@ export function generateEndpointExample({
return {
type: "success",
example: {
id: hash(result),
id: hashJSON(result),
url: getUrlForExample(endpoint, result),
...result
},
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/generation/ir-generator/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Required for ES2017 compatibility.
import "string.prototype.replaceall";

export { constructCasingsGenerator, type CasingsGenerator } from "./casings/CasingsGenerator";
export { constructHttpPath } from "./converters/services/constructHttpPath";
export { convertResponseErrors } from "./converters/services/convertResponseErrors";
Expand Down
11 changes: 11 additions & 0 deletions packages/cli/generation/ir-generator/src/utils/hashJSON.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function hashJSON(obj: unknown): string {
const jsonString = JSON.stringify(obj);
let hash = 0x811c9dc5; // Random prime number.
for (let i = 0; i < jsonString.length; i++) {
const char = jsonString.charCodeAt(i);
hash ^= char;
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
}
const positiveHash = hash >>> 0;
return positiveHash.toString(16);
}
Loading

0 comments on commit 046c466

Please sign in to comment.