Skip to content

Commit

Permalink
tsparser: support root path for enpoints (#1591)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredr authored Nov 25, 2024
1 parent a1c5114 commit e487a9b
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 1 deletion.
41 changes: 41 additions & 0 deletions internal/clientgen/testdata/tsapp/expected_golang.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions internal/clientgen/testdata/tsapp/expected_javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ class SvcServiceClient {

await this.baseClient.callAPI("POST", `/dummy`, JSON.stringify(body), {headers, query})
}

async root(params) {
// Convert our params into the objects we need for the request
const headers = makeRecord({
baz: params.headerBaz,
num: params.headerNum === undefined ? undefined : String(params.headerNum),
})

const query = makeRecord({
bar: params.queryBar,
foo: params.queryFoo === undefined ? undefined : String(params.queryFoo),
})

// Construct the body with only the fields which we want encoded within the body (excluding query string or header fields)
const body = {
baz: params.baz,
foo: params.foo,
}

await this.baseClient.callAPI("POST", `/`, JSON.stringify(body), {headers, query})
}
}

export const svc = {
Expand Down
38 changes: 38 additions & 0 deletions internal/clientgen/testdata/tsapp/expected_typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ export namespace svc {
headerNum?: number
}

export interface Request {
/**
* Foo is good
*/
foo?: number

/**
* Baz is better
*/
baz: string

queryFoo?: boolean
queryBar?: string
headerBaz?: string
headerNum?: number
}

export class ServiceClient {
private baseClient: BaseClient

Expand Down Expand Up @@ -117,6 +134,27 @@ export namespace svc {

await this.baseClient.callAPI("POST", `/dummy`, JSON.stringify(body), {headers, query})
}

public async root(params: Request): Promise<void> {
// Convert our params into the objects we need for the request
const headers = makeRecord<string, string>({
baz: params.headerBaz,
num: params.headerNum === undefined ? undefined : String(params.headerNum),
})

const query = makeRecord<string, string | string[]>({
bar: params.queryBar,
foo: params.queryFoo === undefined ? undefined : String(params.queryFoo),
})

// Construct the body with only the fields which we want encoded within the body (excluding query string or header fields)
const body: Record<string, any> = {
baz: params.baz,
foo: params.foo,
}

await this.baseClient.callAPI("POST", `/`, JSON.stringify(body), {headers, query})
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions internal/clientgen/testdata/tsapp/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ interface UnusedType {
foo: Foo;
}

export const root = api(
{ expose: true, method: "POST", path: "/" },
async (req: Request) => { },
);

export const dummy = api(
{ expose: true, method: "POST", path: "/dummy" },
async (req: Request) => { },
Expand Down
3 changes: 2 additions & 1 deletion tsparser/src/parser/respath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl Path {
// Validate the segments.
for (idx, seg) in segments.iter().enumerate() {
match seg {
Segment::Literal(lit) if lit.is_empty() => {
Segment::Literal(lit) if lit.is_empty() && segments.len() > 1 => {
anyhow::bail!("invalid path: literal cannot be empty");
}
Segment::Param { name, .. } if name.is_empty() => {
Expand Down Expand Up @@ -215,6 +215,7 @@ mod tests {
#[test]
fn test_parse() {
let tests = vec![
("/", Ok(vec![Segment::Literal("".to_string())])),
("/foo", Ok(vec![Segment::Literal("foo".to_string())])),
(
"/foo/bar",
Expand Down

0 comments on commit e487a9b

Please sign in to comment.