Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: add a getDefinition function to a fernworkspace #3784

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ export async function registerWorkspacesV1({
context.failWithoutThrowing("Registering from OpenAPI not currently supported.");
return;
}
const workspaceDefinition = await workspace.getDefinition();
const registerApiResponse = await fiddle.definitionRegistry.registerUsingOrgToken({
apiId: FernFiddle.ApiId(workspace.definition.rootApiFile.contents.name),
apiId: FernFiddle.ApiId(workspaceDefinition.rootApiFile.contents.name),
version,
cliVersion: cliContext.environment.packageVersion,
yamlSchemaVersion: `${YAML_SCHEMA_VERSION}`
Expand All @@ -67,7 +68,7 @@ export async function registerWorkspacesV1({
await axios.put(registerApiResponse.body.definitionS3UploadUrl, await readFile(tarPath));

context.logger.info(
`Registered @${project.config.organization}/${workspace.definition.rootApiFile.contents.name}:${registerApiResponse.body.version}`
`Registered @${project.config.organization}/${workspaceDefinition.rootApiFile.contents.name}:${registerApiResponse.body.version}`
);
});
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ async function writeDefinitionForFernWorkspace({
workspace: FernWorkspace;
context: TaskContext;
}): Promise<void> {
for (const [relativePath, definition] of Object.entries(workspace.definition.importedDefinitions)) {
const workspaceDefinition = await workspace.getDefinition();
for (const [relativePath, definition] of Object.entries(workspaceDefinition.importedDefinitions)) {
const absolutePathToOutputDirectory = join(
workspace.absoluteFilepath,
RelativeFilePath.of(DEFINITION_DIRECTORY),
Expand Down Expand Up @@ -75,7 +76,7 @@ async function writeDefinitionForOpenAPIWorkspace({
RelativeFilePath.of(`.${DEFINITION_DIRECTORY}`)
);
await writeFernDefinition({
definition: fernWorkspace.definition,
definition: await fernWorkspace.getDefinition(),
absolutePathToOutputDirectory
});
context.logger.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
ExampleHeader,
ExampleInlinedRequestBodyProperty,
ExamplePathParameter,
ExampleWebSocketMessage,
ExampleWebSocketMessageBody,
ExampleWebSocketSession,
Name,
Expand Down Expand Up @@ -103,75 +102,81 @@ export async function convertChannel({
)
: [],
messages: Object.values(messages),
examples: (channel.examples ?? []).map((example): ExampleWebSocketSession => {
const convertedPathParameters = convertChannelPathParameters({
channel,
example,
typeResolver,
exampleResolver,
variableResolver,
file,
workspace
});
return {
name: example.name != null ? file.casingsGenerator.generateName(example.name) : undefined,
docs: example.docs,
url: buildUrl({ channel, example, pathParams: convertedPathParameters }),
...convertedPathParameters,
...convertHeaders({ channel, example, typeResolver, exampleResolver, file, workspace }),
queryParameters:
example["query-parameters"] != null
? Object.entries(example["query-parameters"]).map(([wireKey, value]) => {
const queryParameterDeclaration = channel["query-parameters"]?.[wireKey];
if (queryParameterDeclaration == null) {
throw new Error(`Query parameter ${wireKey} does not exist`);
}
return {
name: file.casingsGenerator.generateNameAndWireValue({
name: getQueryParameterName({
queryParameterKey: wireKey,
queryParameter: queryParameterDeclaration
}).name,
wireValue: wireKey
}),
value: convertTypeReferenceExample({
example: value,
rawTypeBeingExemplified:
typeof queryParameterDeclaration === "string"
? queryParameterDeclaration
: queryParameterDeclaration.type,
typeResolver,
exampleResolver,
fileContainingRawTypeReference: file,
fileContainingExample: file,
workspace
examples: await Promise.all(
(channel.examples ?? []).map(async (example): Promise<Promise<ExampleWebSocketSession>> => {
const convertedPathParameters = await convertChannelPathParameters({
channel,
example,
typeResolver,
exampleResolver,
variableResolver,
file,
workspace
});
return {
name: example.name != null ? file.casingsGenerator.generateName(example.name) : undefined,
docs: example.docs,
url: buildUrl({ channel, example, pathParams: convertedPathParameters }),
...convertedPathParameters,
...(await convertHeaders({ channel, example, typeResolver, exampleResolver, file, workspace })),
queryParameters:
example["query-parameters"] != null
? await Promise.all(
Object.entries(example["query-parameters"]).map(async ([wireKey, value]) => {
const queryParameterDeclaration = channel["query-parameters"]?.[wireKey];
if (queryParameterDeclaration == null) {
throw new Error(`Query parameter ${wireKey} does not exist`);
}
return {
name: file.casingsGenerator.generateNameAndWireValue({
name: getQueryParameterName({
queryParameterKey: wireKey,
queryParameter: queryParameterDeclaration
}).name,
wireValue: wireKey
}),
value: await convertTypeReferenceExample({
example: value,
rawTypeBeingExemplified:
typeof queryParameterDeclaration === "string"
? queryParameterDeclaration
: queryParameterDeclaration.type,
typeResolver,
exampleResolver,
fileContainingRawTypeReference: file,
fileContainingExample: file,
workspace
})
};
})
};
})
: [],
messages: example.messages.map((messageExample): ExampleWebSocketMessage => {
const message = channel.messages?.[messageExample.type];
if (message == null) {
throw new Error(`Message ${messageExample.type} does not exist`);
}
return {
type: messageExample.type,
body: convertExampleWebSocketMessageBody({
message,
example: messageExample.body,
typeResolver,
exampleResolver,
file,
workspace
)
: [],
messages: await Promise.all(
example.messages.map(async (messageExample) => {
const message = channel.messages?.[messageExample.type];
if (message == null) {
throw new Error(`Message ${messageExample.type} does not exist`);
}
return {
type: messageExample.type,
body: await convertExampleWebSocketMessageBody({
message,
example: messageExample.body,
typeResolver,
exampleResolver,
file,
workspace
})
};
})
};
})
};
})
)
};
})
)
};
}

function convertExampleWebSocketMessageBody({
async function convertExampleWebSocketMessageBody({
message,
example,
typeResolver,
Expand All @@ -185,10 +190,10 @@ function convertExampleWebSocketMessageBody({
exampleResolver: ExampleResolver;
file: FernFileContext;
workspace: FernWorkspace;
}): ExampleWebSocketMessageBody {
}): Promise<ExampleWebSocketMessageBody> {
if (!isInlineMessageBody(message.body)) {
return ExampleWebSocketMessageBody.reference(
convertTypeReferenceExample({
await convertTypeReferenceExample({
example,
rawTypeBeingExemplified: typeof message.body !== "string" ? message.body.type : message.body,
typeResolver,
Expand All @@ -213,7 +218,7 @@ function convertExampleWebSocketMessageBody({
name: getPropertyName({ propertyKey: wireKey, property: inlinedRequestPropertyDeclaration }).name,
wireValue: wireKey
}),
value: convertTypeReferenceExample({
value: await convertTypeReferenceExample({
example: propertyExample,
rawTypeBeingExemplified:
typeof inlinedRequestPropertyDeclaration !== "string"
Expand All @@ -228,7 +233,7 @@ function convertExampleWebSocketMessageBody({
originalTypeDeclaration: undefined
});
} else {
const originalTypeDeclaration = getOriginalTypeDeclarationForPropertyFromExtensions({
const originalTypeDeclaration = await getOriginalTypeDeclarationForPropertyFromExtensions({
extends_: message.body.extends,
wirePropertyKey: wireKey,
typeResolver,
Expand All @@ -243,7 +248,7 @@ function convertExampleWebSocketMessageBody({
.name,
wireValue: wireKey
}),
value: convertTypeReferenceExample({
value: await convertTypeReferenceExample({
example: propertyExample,
rawTypeBeingExemplified:
typeof originalTypeDeclaration.rawPropertyType === "string"
Expand All @@ -261,7 +266,7 @@ function convertExampleWebSocketMessageBody({
}

return ExampleWebSocketMessageBody.inlinedBody({
jsonExample: exampleResolver.resolveAllReferencesInExampleOrThrow({ example, file }).resolvedExample,
jsonExample: (await exampleResolver.resolveAllReferencesInExampleOrThrow({ example, file })).resolvedExample,
properties: exampleProperties
});
}
Expand Down Expand Up @@ -300,7 +305,7 @@ export function isReferencedWebhookPayloadSchema(
return (payload as RawSchemas.WebSocketChannelReferencedMessageSchema).type != null;
}

function convertChannelPathParameters({
async function convertChannelPathParameters({
channel,
example,
typeResolver,
Expand All @@ -316,10 +321,10 @@ function convertChannelPathParameters({
variableResolver: VariableResolver;
file: FernFileContext;
workspace: FernWorkspace;
}): Pick<ExampleWebSocketSession, "pathParameters"> {
}): Promise<Pick<ExampleWebSocketSession, "pathParameters">> {
const pathParameters: ExamplePathParameter[] = [];

const buildExamplePathParameter = ({
const buildExamplePathParameter = async ({
name,
pathParameterDeclaration,
examplePathParameter
Expand All @@ -335,7 +340,7 @@ function convertChannelPathParameters({
});
return {
name,
value: convertTypeReferenceExample({
value: await convertTypeReferenceExample({
example: examplePathParameter,
rawTypeBeingExemplified: resolvedPathParameter.rawType,
typeResolver,
Expand All @@ -354,7 +359,7 @@ function convertChannelPathParameters({

if (pathParameterDeclaration != null) {
pathParameters.push(
buildExamplePathParameter({
await buildExamplePathParameter({
name: file.casingsGenerator.generateName(key),
pathParameterDeclaration,
examplePathParameter
Expand All @@ -369,7 +374,7 @@ function convertChannelPathParameters({
return { pathParameters };
}

function convertHeaders({
async function convertHeaders({
channel,
example,
typeResolver,
Expand All @@ -383,7 +388,7 @@ function convertHeaders({
exampleResolver: ExampleResolver;
file: FernFileContext;
workspace: FernWorkspace;
}): Pick<ExampleWebSocketSession, "headers"> {
}): Promise<Pick<ExampleWebSocketSession, "headers">> {
const headers: ExampleHeader[] = [];

if (example.headers != null) {
Expand All @@ -395,7 +400,7 @@ function convertHeaders({
name: getHeaderName({ headerKey: wireKey, header: headerDeclaration }).name,
wireValue: wireKey
}),
value: convertTypeReferenceExample({
value: await convertTypeReferenceExample({
example: exampleHeader,
rawTypeBeingExemplified:
typeof headerDeclaration === "string" ? headerDeclaration : headerDeclaration.type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TypeResolver } from "../resolvers/TypeResolver";
import { parseErrorName } from "../utils/parseErrorName";
import { convertTypeReferenceExample } from "./type-declarations/convertExampleType";

export function convertErrorDeclaration({
export async function convertErrorDeclaration({
errorName,
errorDeclaration,
file,
Expand All @@ -21,18 +21,20 @@ export function convertErrorDeclaration({
typeResolver: TypeResolver;
exampleResolver: ExampleResolver;
workspace: FernWorkspace;
}): ErrorDeclaration {
}): Promise<ErrorDeclaration> {
const examples: FernIr.ExampleError[] = [];
if (errorDeclaration.type != null && errorDeclaration.examples != null) {
for (const example of errorDeclaration.examples) {
examples.push({
name: example.name != null ? file.casingsGenerator.generateName(example.name) : undefined,
docs: example.docs,
jsonExample: exampleResolver.resolveAllReferencesInExampleOrThrow({
example: example.value,
file
}).resolvedExample,
shape: convertTypeReferenceExample({
jsonExample: (
await exampleResolver.resolveAllReferencesInExampleOrThrow({
example: example.value,
file
})
).resolvedExample,
shape: await convertTypeReferenceExample({
example: example.value,
rawTypeBeingExemplified: errorDeclaration.type,
fileContainingRawTypeReference: file,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function convertOAuthRefreshEndpoint({
file: FernFileContext;
refreshTokenEndpoint: RefreshTokenEndpoint;
}): Promise<OAuthRefreshEndpoint | undefined> {
const resolvedEndpoint = endpointResolver.resolveEndpointOrThrow({
const resolvedEndpoint = await endpointResolver.resolveEndpointOrThrow({
endpoint: refreshTokenEndpoint.endpoint,
file
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function convertOAuthTokenEndpoint({
file: FernFileContext;
tokenEndpoint: TokenEndpoint;
}): Promise<OAuthTokenEndpoint | undefined> {
const resolvedEndpoint = endpointResolver.resolveEndpointOrThrow({
const resolvedEndpoint = await endpointResolver.resolveEndpointOrThrow({
endpoint: tokenEndpoint.endpoint,
file
});
Expand Down
Loading
Loading