Skip to content

Commit

Permalink
improvement: add in a version field to the register function (#646)
Browse files Browse the repository at this point in the history
  • Loading branch information
armandobelardo authored Apr 15, 2024
1 parent 30ff892 commit 7a2b8d7
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 16 deletions.
5 changes: 5 additions & 0 deletions fern/apis/fdr/definition/api/v1/register/__package__.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,27 @@ types:
TypescriptPackage:
properties:
package: string
version: optional<string>

PythonPackage:
properties:
package: string
version: optional<string>

GoModule:
properties:
githubRepo: string
version: optional<string>

JavaCoordinate:
properties:
coordinate: string
version: optional<string>

RubyGem:
properties:
gem: string
version: optional<string>

ApiDefinitionPackage:
properties:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Sdk" ADD COLUMN "version" TEXT;
2 changes: 2 additions & 0 deletions servers/fdr/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ model Sdk {
// Optional for backcompat
// Package name used by registry (e.g. @velllum/api or com.vellum.ai:vellum)
package String?
// Optional for backcompat
version String?
language Language
sdk Bytes
createdAt DateTime @default(now())
Expand Down
40 changes: 35 additions & 5 deletions servers/fdr/src/__test__/db/db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,31 @@ it("snippets dao", async () => {
},
},
});
await serverApp?.dao.snippets().storeSnippets({
storeSnippetsInfo: {
orgId: "acme",
apiId: "api",
sdk: {
type: "python",
sdk: {
package: "acme",
version: "0.0.2",
},
snippets: [
{
endpoint: {
path: "/users/v1",
method: FdrAPI.EndpointMethod.Get,
},
snippet: {
async_client: "client = AsyncAcme(api_key='YOUR_API_KEY')",
sync_client: "client = Acme(api_key='YOUR_API_KEY')",
},
},
],
},
},
});
// get snippets
const response = await serverApp?.dao.snippets().loadSnippetsPage({
loadSnippetsInfo: {
Expand Down Expand Up @@ -456,22 +481,27 @@ it("snippets dao", async () => {
throw new Error("expected a python snippet");
}
expect(snippet.sdk.package).toEqual("acme");
expect(snippet.sdk.version).toEqual("0.0.1");
expect(snippet.sdk.version).toEqual("0.0.2");
expect(snippet.async_client).toEqual("client = AsyncAcme(api_key='YOUR_API_KEY')");
expect(snippet.sync_client).toEqual("client = Acme(api_key='YOUR_API_KEY')");

const sdkId = await serverApp?.dao.sdks().getLatestSdkIdForPackage({ sdkPackage: "acme", language: "PYTHON" });
expect(sdkId).toEqual("python|acme|0.0.1");
const sdkId = await serverApp?.dao.sdks().getSdkIdForPackage({ sdkPackage: "acme", language: "PYTHON" });
expect(sdkId).toEqual("python|acme|0.0.2");

const sdkIdPrevious = await serverApp?.dao
.sdks()
.getSdkIdForPackage({ sdkPackage: "acme", language: "PYTHON", version: "0.0.1" });
expect(sdkIdPrevious).toEqual("python|acme|0.0.2");

const snippetsForSdkId = await serverApp?.dao.snippets().loadAllSnippetsForSdkIds(sdkId != null ? [sdkId] : []);
expect(snippetsForSdkId).toEqual({
"python|acme|0.0.1": {
"python|acme|0.0.2": {
"/users/v1": {
DELETE: [],
GET: [
{
async_client: "client = AsyncAcme(api_key='YOUR_API_KEY')",
sdk: { package: "acme", version: "0.0.1" },
sdk: { package: "acme", version: "0.0.2" },
sync_client: "client = Acme(api_key='YOUR_API_KEY')",
type: "python",
},
Expand Down
5 changes: 2 additions & 3 deletions servers/fdr/src/controllers/api/getRegisterApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ export function getRegisterApiService(app: FdrApplication): APIV1WriteService {
orgId: req.body.orgId,
});
const snippetsConfiguration = req.body.definition.snippetsConfiguration ?? {};
const snippetsConfigurationWithSdkIds = await app.dao
.sdks()
.getLatestSdkIdsForPackages(snippetsConfiguration);

const snippetsConfigurationWithSdkIds = await app.dao.sdks().getSdkIdsForPackages(snippetsConfiguration);
const sdkIds = [];
if (snippetsConfigurationWithSdkIds.typescriptSdk != null) {
sdkIds.push(snippetsConfigurationWithSdkIds.typescriptSdk.sdkId);
Expand Down
49 changes: 41 additions & 8 deletions servers/fdr/src/db/sdk/SdkDao.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { APIV1Write } from "@fern-api/fdr-sdk";
import { Language, PrismaClient } from "@prisma/client";
import { SdkIdFactory } from "../snippets/SdkIdFactory";
import { SdkId } from "../types";

export interface SdkIdForPackage {
Expand All @@ -11,53 +12,59 @@ export interface SdkIdForPackage {
}

export interface SdkDao {
getLatestSdkIdsForPackages(snippetConfig: APIV1Write.SnippetsConfig): Promise<SdkIdForPackage>;
getSdkIdsForPackages(snippetConfig: APIV1Write.SnippetsConfig): Promise<SdkIdForPackage>;

getLatestSdkIdForPackage({
getSdkIdForPackage({
sdkPackage,
language,
version,
}: {
sdkPackage: string;
language: Language;
version?: string;
}): Promise<SdkId | undefined>;
}

export class SdkDaoImpl implements SdkDao {
constructor(private readonly prisma: PrismaClient) {}

public async getLatestSdkIdsForPackages(snippetConfig: APIV1Write.SnippetsConfig): Promise<SdkIdForPackage> {
public async getSdkIdsForPackages(snippetConfig: APIV1Write.SnippetsConfig): Promise<SdkIdForPackage> {
const result: SdkIdForPackage = {};
if (snippetConfig.typescriptSdk != null) {
const sdkId = await this.getLatestSdkIdForPackage({
const sdkId = await this.getSdkIdForPackage({
sdkPackage: snippetConfig.typescriptSdk.package,
language: Language.TYPESCRIPT,
version: snippetConfig.typescriptSdk.version,
});
if (sdkId != null) {
result.typescriptSdk = { ...snippetConfig.typescriptSdk, sdkId };
}
}
if (snippetConfig.pythonSdk != null) {
const sdkId = await this.getLatestSdkIdForPackage({
const sdkId = await this.getSdkIdForPackage({
sdkPackage: snippetConfig.pythonSdk.package,
language: Language.PYTHON,
version: snippetConfig.pythonSdk.version,
});
if (sdkId != null) {
result.pythonSdk = { ...snippetConfig.pythonSdk, sdkId };
}
}
if (snippetConfig.javaSdk != null) {
const sdkId = await this.getLatestSdkIdForPackage({
const sdkId = await this.getSdkIdForPackage({
sdkPackage: snippetConfig.javaSdk.coordinate,
language: Language.JAVA,
version: snippetConfig.javaSdk.version,
});
if (sdkId != null) {
result.javaSdk = { ...snippetConfig.javaSdk, sdkId };
}
}
if (snippetConfig.goSdk != null) {
const sdkId = await this.getLatestSdkIdForPackage({
const sdkId = await this.getSdkIdForPackage({
sdkPackage: snippetConfig.goSdk.githubRepo,
language: Language.GO,
version: snippetConfig.goSdk.version,
});
if (sdkId != null) {
result.goSdk = { ...snippetConfig.goSdk, sdkId };
Expand All @@ -67,19 +74,45 @@ export class SdkDaoImpl implements SdkDao {
return result;
}

public async getLatestSdkIdForPackage({
public async getSdkIdForPackage({
sdkPackage,
language,
version,
}: {
sdkPackage: string;
language: Language;
version?: string;
}): Promise<string | undefined> {
let id: string | undefined;
if (version != null) {
switch (language) {
case Language.TYPESCRIPT:
id = SdkIdFactory.fromTypescript({ package: sdkPackage, version });
break;
case Language.PYTHON:
id = SdkIdFactory.fromPython({ package: sdkPackage, version });
break;
case Language.GO:
id = SdkIdFactory.fromGo({ githubRepo: sdkPackage, version });
break;
case Language.RUBY:
id = SdkIdFactory.fromRuby({ gem: sdkPackage, version });
break;
default:
break;
}
if (id != null) {
return id;
}
}

const sdkRow = await this.prisma.sdk.findFirst({
select: {
id: true,
},
where: {
package: sdkPackage,
version,
language,
},
orderBy: {
Expand Down
1 change: 1 addition & 0 deletions servers/fdr/src/db/snippets/SnippetsDao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export class SnippetsDaoImpl implements SnippetsDao {
data: {
id: sdkInfo.id,
package: getPackageNameFromSdkSnippetsCreate(storeSnippetsInfo.sdk),
version: storeSnippetsInfo.sdk.sdk.version,
language: sdkInfo.language,
sdk: writeBuffer(storeSnippetsInfo.sdk.sdk),
},
Expand Down

0 comments on commit 7a2b8d7

Please sign in to comment.