Skip to content
Merged
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
36 changes: 28 additions & 8 deletions src/generators/typescript/fetch/project_files/runtime.ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ export interface ConfigurationParameters {
}

export class Configuration {
constructor(private configuration: ConfigurationParameters = {}) {}
private configuration: ConfigurationParameters;

set config(configuration: Configuration) {
constructor(configuration: ConfigurationParameters = {}) {
this.configuration = configuration;
}

set config(configuration: ConfigurationParameters) {
this.configuration = configuration;
}

Expand Down Expand Up @@ -85,9 +89,11 @@ export const DefaultConfig = new Configuration();
export class BaseAPI {

private static readonly jsonRegex = new RegExp('^(:?application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(:?;.*)?$', 'i');
protected configuration: Configuration;
private middleware: Middleware[];

constructor(protected configuration = DefaultConfig) {
constructor(configuration: Configuration = DefaultConfig) {
this.configuration = configuration;
this.middleware = configuration.middleware;
}

Expand Down Expand Up @@ -266,22 +272,31 @@ function isFormData(value: unknown): value is FormData {

export class ResponseError extends Error {
override name = "ResponseError" as const;
constructor(public response: Response, msg?: string) {
public response: Response;

constructor(response: Response, msg?: string) {
super(msg);
this.response = response;
}
}

export class FetchError extends Error {
override name = "FetchError" as const;
constructor(public cause: Error, msg?: string) {
public cause: Error;

constructor(cause: Error, msg?: string) {
super(msg);
this.cause = cause;
}
}

export class RequiredError extends Error {
override name = "RequiredError" as const;
constructor(public field: string, msg?: string) {
public field: string;

constructor(field: string, msg?: string) {
super(msg);
this.field = field;
}
}

Expand Down Expand Up @@ -405,8 +420,10 @@ class ApiResponseBase {
public readonly ok: boolean;
public readonly statusText: string;
public readonly headers: Headers;
public readonly raw: Response;

constructor(public readonly raw: Response) {
constructor(raw: Response) {
this.raw = raw;
this.status = raw.status;
this.ok = raw.ok;
this.statusText = raw.statusText;
Expand All @@ -415,8 +432,11 @@ class ApiResponseBase {
}

export class JSONApiResponse<T> extends ApiResponseBase {
constructor(raw: Response, private transformer: ResponseTransformer<T> = (jsonValue: unknown) => jsonValue as T) {
private transformer: ResponseTransformer<T>;

constructor(raw: Response, transformer: ResponseTransformer<T> = (jsonValue: unknown) => jsonValue as T) {
super(raw);
this.transformer = transformer;
}

async value(): Promise<T> {
Expand Down
34 changes: 32 additions & 2 deletions src/generators/typescript/fetch/sigil_emit_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,13 +1160,21 @@ fn build_convenience_method(op: &IrOperation) -> Result<FunSpec, String> {
}

fn is_void_type(ty: &TypeName) -> bool {
is_primitive_type(ty, "void")
}

fn is_unknown_type(ty: &TypeName) -> bool {
is_primitive_type(ty, "unknown")
}

fn is_primitive_type(ty: &TypeName, primitive: &str) -> bool {
let Ok(val) = serde_json::to_value(ty) else {
return false;
};
let Ok(void_val) = serde_json::to_value(TypeName::primitive("void")) else {
let Ok(primitive_val) = serde_json::to_value(TypeName::primitive(primitive)) else {
return false;
};
val == void_val
val == primitive_val
}

// ============================================================================
Expand Down Expand Up @@ -1312,6 +1320,9 @@ fn dedup_union_members(members: Vec<TypeName>) -> Vec<TypeName> {

fn dedup_union(members: Vec<TypeName>) -> TypeName {
let mut out = dedup_union_members(members);
if out.iter().any(is_unknown_type) {
return TypeName::primitive("unknown");
}
if out.len() == 1 {
out.pop().unwrap()
} else {
Expand Down Expand Up @@ -1777,3 +1788,22 @@ fn collect_convertible_named_refs(
_ => {}
}
}

#[cfg(test)]
mod tests {
use super::*;

fn type_json(ty: &TypeName) -> serde_json::Value {
serde_json::to_value(ty).expect("TypeName serializes")
}

#[test]
fn dedup_union_collapses_unknown_members() {
let ty = dedup_union(vec![
TypeName::primitive("unknown"),
TypeName::importable_type("../models/ErrorResponse", "ErrorResponse"),
]);

assert_eq!(type_json(&ty), type_json(&TypeName::primitive("unknown")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ export interface ConfigurationParameters {
}

export class Configuration {
constructor(private configuration: ConfigurationParameters = {}) {}
private configuration: ConfigurationParameters;

set config(configuration: Configuration) {
constructor(configuration: ConfigurationParameters = {}) {
this.configuration = configuration;
}

set config(configuration: ConfigurationParameters) {
this.configuration = configuration;
}

Expand Down Expand Up @@ -91,9 +95,11 @@ export const DefaultConfig = new Configuration();
export class BaseAPI {

private static readonly jsonRegex = new RegExp('^(:?application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(:?;.*)?$', 'i');
protected configuration: Configuration;
private middleware: Middleware[];

constructor(protected configuration = DefaultConfig) {
constructor(configuration: Configuration = DefaultConfig) {
this.configuration = configuration;
this.middleware = configuration.middleware;
}

Expand Down Expand Up @@ -272,22 +278,31 @@ function isFormData(value: unknown): value is FormData {

export class ResponseError extends Error {
override name = "ResponseError" as const;
constructor(public response: Response, msg?: string) {
public response: Response;

constructor(response: Response, msg?: string) {
super(msg);
this.response = response;
}
}

export class FetchError extends Error {
override name = "FetchError" as const;
constructor(public cause: Error, msg?: string) {
public cause: Error;

constructor(cause: Error, msg?: string) {
super(msg);
this.cause = cause;
}
}

export class RequiredError extends Error {
override name = "RequiredError" as const;
constructor(public field: string, msg?: string) {
public field: string;

constructor(field: string, msg?: string) {
super(msg);
this.field = field;
}
}

Expand Down Expand Up @@ -411,8 +426,10 @@ class ApiResponseBase {
public readonly ok: boolean;
public readonly statusText: string;
public readonly headers: Headers;
public readonly raw: Response;

constructor(public readonly raw: Response) {
constructor(raw: Response) {
this.raw = raw;
this.status = raw.status;
this.ok = raw.ok;
this.statusText = raw.statusText;
Expand All @@ -421,8 +438,11 @@ class ApiResponseBase {
}

export class JSONApiResponse<T> extends ApiResponseBase {
constructor(raw: Response, private transformer: ResponseTransformer<T> = (jsonValue: unknown) => jsonValue as T) {
private transformer: ResponseTransformer<T>;

constructor(raw: Response, transformer: ResponseTransformer<T> = (jsonValue: unknown) => jsonValue as T) {
super(raw);
this.transformer = transformer;
}

async value(): Promise<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ export interface ConfigurationParameters {
}

export class Configuration {
constructor(private configuration: ConfigurationParameters = {}) {}
private configuration: ConfigurationParameters;

set config(configuration: Configuration) {
constructor(configuration: ConfigurationParameters = {}) {
this.configuration = configuration;
}

set config(configuration: ConfigurationParameters) {
this.configuration = configuration;
}

Expand Down Expand Up @@ -124,9 +128,11 @@ function isFile(value: UploadFileInput): value is File {
export class BaseAPI {

private static readonly jsonRegex = new RegExp('^(:?application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(:?;.*)?$', 'i');
protected configuration: Configuration;
private middleware: Middleware[];

constructor(protected configuration = DefaultConfig) {
constructor(configuration: Configuration = DefaultConfig) {
this.configuration = configuration;
this.middleware = configuration.middleware;
}

Expand Down Expand Up @@ -305,22 +311,31 @@ function isFormData(value: unknown): value is FormData {

export class ResponseError extends Error {
override name = "ResponseError" as const;
constructor(public response: Response, msg?: string) {
public response: Response;

constructor(response: Response, msg?: string) {
super(msg);
this.response = response;
}
}

export class FetchError extends Error {
override name = "FetchError" as const;
constructor(public cause: Error, msg?: string) {
public cause: Error;

constructor(cause: Error, msg?: string) {
super(msg);
this.cause = cause;
}
}

export class RequiredError extends Error {
override name = "RequiredError" as const;
constructor(public field: string, msg?: string) {
public field: string;

constructor(field: string, msg?: string) {
super(msg);
this.field = field;
}
}

Expand Down Expand Up @@ -444,8 +459,10 @@ class ApiResponseBase {
public readonly ok: boolean;
public readonly statusText: string;
public readonly headers: Headers;
public readonly raw: Response;

constructor(public readonly raw: Response) {
constructor(raw: Response) {
this.raw = raw;
this.status = raw.status;
this.ok = raw.ok;
this.statusText = raw.statusText;
Expand All @@ -454,8 +471,11 @@ class ApiResponseBase {
}

export class JSONApiResponse<T> extends ApiResponseBase {
constructor(raw: Response, private transformer: ResponseTransformer<T> = (jsonValue: unknown) => jsonValue as T) {
private transformer: ResponseTransformer<T>;

constructor(raw: Response, transformer: ResponseTransformer<T> = (jsonValue: unknown) => jsonValue as T) {
super(raw);
this.transformer = transformer;
}

async value(): Promise<T> {
Expand Down
Loading
Loading