Skip to content

Commit

Permalink
fix js field camel-casing
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jul 13, 2020
1 parent c70c5a6 commit 30e7e1d
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 39 deletions.
91 changes: 55 additions & 36 deletions generators/tsclient/testdata/todo_client.ts
@@ -1,39 +1,58 @@

let fetch = typeof window !== 'undefined' ? window.fetch : null
if(!fetch) {
fetch = require('node-fetch')
}
// fetch for Node
const fetch = (typeof window == 'undefined' || window.fetch == null)
// @ts-ignore
? require('node-fetch')
: window.fetch

/**
* ClientError is an API client error providing the HTTP status code and error type.
*/

class ClientError extends Error {
status: number;
type?: string;

constructor(status: number, message?: string, type?: string) {
super(message)
this.status = status
this.type = type
}
}

/**
* Call method with params via a POST request.
*/

async function call(url: string, authToken: string, method: string, params?: any): Promise<string> {
const res = await fetch(url + '/' + method, {
method: 'POST',
body: JSON.stringify(params),
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${authToken}`
}
})

// we have an error, try to parse a well-formed json
// error response, otherwise default to status code
if (res.status >= 300) {
let err
try {
const { type, message } = await res.json()
err = new Error(message)
err.type = type
} catch {
err = new Error(`${res.status} ${res.statusText}`)
}
throw err
}

return res.text()
* Call method with params via a POST request.
*/

async function call(url: string, method: string, authToken?: string, params?: any): Promise<string> {
const headers: Record<string, string> = {
'Content-Type': 'application/json'
}

if (authToken != null) {
headers['Authorization'] = `Bearer ${authToken}`
}

const res = await fetch(url + '/' + method, {
method: 'POST',
body: JSON.stringify(params),
headers
})

// we have an error, try to parse a well-formed json
// error response, otherwise default to status code
if (res.status >= 300) {
let err
try {
const { type, message } = await res.json()
err = new ClientError(res.status, message, type)
} catch {
err = new ClientError(res.status, res.statusText)
}
throw err
}

return res.text()
}


Expand All @@ -46,7 +65,7 @@ const reISO8601 = /(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\
export class Client {

private url: string
private authToken: string
private authToken?: string

/**
* Initialize.
Expand All @@ -72,15 +91,15 @@ export class Client {
*/

async addItem(params: AddItemInput) {
await call(this.url, this.authToken, 'add_item', params)
await call(this.url, 'add_item', this.authToken, params)
}

/**
* getItems: Return all items in the list.
*/

async getItems(): Promise<GetItemsOutput> {
let res = await call(this.url, this.authToken, 'get_items')
let res = await call(this.url, 'get_items', this.authToken)
let out: GetItemsOutput = JSON.parse(res, this.decoder)
return out
}
Expand All @@ -90,7 +109,7 @@ export class Client {
*/

async removeItem(params: RemoveItemInput) {
await call(this.url, this.authToken, 'remove_item', params)
await call(this.url, 'remove_item', this.authToken, params)
}

}
2 changes: 2 additions & 0 deletions generators/tsclient/tsclient_test.go
Expand Up @@ -21,5 +21,7 @@ func TestGenerate(t *testing.T) {
err = tsclient.Generate(&act, schema, "node-fetch")
assert.NoError(t, err, "generating")

// ioutil.WriteFile("testdata/todo_client.ts", act.Bytes(), 0755)

assert.Equal(t, string(exp), act.String())
}
30 changes: 30 additions & 0 deletions generators/tstypes/testdata/todo_types.ts
@@ -0,0 +1,30 @@
// Item A to-do item.
interface Item {
// createdAt is the time the to-do item was created.
createdAt?: Date

// id is the id of the item. This field is read-only.
id?: number

// text is the to-do item text. This field is required.
text: string
}

// AddItemInput params.
interface AddItemInput {
// item is the item to add. This field is required.
item: string
}

// GetItemsOutput params.
interface GetItemsOutput {
// items is the list of to-do items.
items?: Item[]
}

// RemoveItemInput params.
interface RemoveItemInput {
// id is the id of the item to remove.
id?: number
}

7 changes: 4 additions & 3 deletions generators/tstypes/tstypes.go
Expand Up @@ -64,11 +64,12 @@ func writeFields(w io.Writer, s *schema.Schema, fields []schema.Field) {

// writeField to writer.
func writeField(w io.Writer, s *schema.Schema, f schema.Field) {
fmt.Fprintf(w, " // %s is %s%s\n", f.Name, f.Description, schemautil.FormatExtra(f))
name := format.JsName(f.Name)
fmt.Fprintf(w, " // %s is %s%s\n", name, f.Description, schemautil.FormatExtra(f))
if f.Required {
fmt.Fprintf(w, " %s: %s\n", f.Name, jsType(s, f))
fmt.Fprintf(w, " %s: %s\n", name, jsType(s, f))
} else {
fmt.Fprintf(w, " %s?: %s\n", f.Name, jsType(s, f))
fmt.Fprintf(w, " %s?: %s\n", name, jsType(s, f))
}
}

Expand Down
26 changes: 26 additions & 0 deletions generators/tstypes/tstypes_test.go
@@ -1 +1,27 @@
package tstypes_test

import (
"bytes"
"io/ioutil"
"testing"

"github.com/apex/rpc/generators/tstypes"
"github.com/apex/rpc/schema"
"github.com/tj/assert"
)

func TestGenerate(t *testing.T) {
exp, err := ioutil.ReadFile("testdata/todo_types.ts")
assert.NoError(t, err, "reading fixture")

schema, err := schema.Load("../../examples/todo/schema.json")
assert.NoError(t, err, "loading schema")

var act bytes.Buffer
err = tstypes.Generate(&act, schema)
assert.NoError(t, err, "generating")

// ioutil.WriteFile("testdata/todo_types.ts", act.Bytes(), 0755)

assert.Equal(t, string(exp), act.String())
}

0 comments on commit 30e7e1d

Please sign in to comment.