-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generic-google-api-http-definitions
- Loading branch information
Showing
8 changed files
with
160 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Generate generic definitions from [google.api.http](https://cloud.google.com/endpoints/docs/grpc/transcoding) | ||
|
||
To generate `.ts` files for your `.proto` files that contain `google.api.http`, you can use the `--ts_proto_opt=onlyTypes=true,outputServices=generic-google-api-http-definitions` option. | ||
|
||
Please refer to [integration/google-api-http](./integration/google-api-http) for an input/output example. | ||
|
||
## Client implementation example | ||
|
||
```typescript | ||
// This is just an example, please test it to see if it works for you. | ||
function createApi< | ||
S extends Record<string, { path: string; method: string; body?: string; requestType: any; responseType: any }>, | ||
>( | ||
fetcher: (input: { path: string; method: string; body?: string }) => Promise<unknown>, | ||
serviceDef: S, | ||
): { [K in keyof S]: (payload: S[K]["requestType"]) => Promise<S[K]["responseType"]> } { | ||
// @ts-expect-error | ||
return Object.fromEntries( | ||
Object.entries(serviceDef).map(([name, endpointDef]) => { | ||
return [ | ||
name, | ||
async (payload: typeof endpointDef.requestType): Promise<typeof endpointDef.responseType> => { | ||
const bodyKey = endpointDef.body; | ||
const payloadClone = bodyKey === "*" ? JSON.parse(JSON.stringify(payload)) : null; | ||
const path = endpointDef.path.replace(/\{([^\}]+)\}/g, (_, key) => { | ||
delete payloadClone[key]; | ||
return payload[key]; | ||
}); | ||
let body: string | undefined = undefined; | ||
if (bodyKey === "*") { | ||
body = JSON.stringify(payloadClone); | ||
} else if (bodyKey) { | ||
body = JSON.stringify({ [bodyKey]: payload[bodyKey] }); | ||
} | ||
|
||
return fetcher({ path, method: endpointDef.method, body }); | ||
}, | ||
]; | ||
}), | ||
); | ||
} | ||
|
||
const fetcher = (input: { path: string; method: string; body?: string }) => { | ||
const url = "http://localhost:8080" + input.path; | ||
const init: RequestInit = { | ||
method: input.method, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}; | ||
if (input.body) { | ||
init.body = input.body; | ||
} | ||
return fetch(url, init).then((res) => res.json()); | ||
}; | ||
|
||
const api = createApi(fetcher, Messaging); | ||
|
||
api | ||
.GetMessage({ | ||
messageId: "123", | ||
}) | ||
.then((res) => { | ||
console.log(res); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
outputClientImpl=false,outputEncodeMethods=false,outputJsonMethods=false,outputServices=generic-definitions | ||
onlyTypes=true,outputServices=generic-google-api-http-definitions |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
src/generate-generic-google-api-http-service-definition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { ServiceDescriptorProto, MethodOptions } from "ts-proto-descriptors"; | ||
import { Code, code, joinCode } from "ts-poet"; | ||
import { requestType, responseType } from "./types"; | ||
import { assertInstanceOf, FormattedMethodDescriptor, findHttpRule } from "./utils"; | ||
import { Context } from "./context"; | ||
|
||
export function generateGenericGoogleApiHttpServiceDefinition(ctx: Context, serviceDesc: ServiceDescriptorProto): Code { | ||
const chunks: Code[] = []; | ||
|
||
const httpRules: Record<string, ReturnType<typeof findHttpRule> | undefined> = {}; | ||
let hasHttpRule = false; | ||
|
||
serviceDesc.method.forEach((methodDesc) => { | ||
const httpRule = findHttpRule(methodDesc.options?.httpRule); | ||
|
||
if (httpRule) { | ||
hasHttpRule = true; | ||
assertInstanceOf(methodDesc, FormattedMethodDescriptor); | ||
httpRules[methodDesc.formattedName] = httpRule; | ||
} | ||
}); | ||
|
||
if (hasHttpRule) { | ||
chunks.push(code` | ||
export const ${serviceDesc.name} = { | ||
`); | ||
|
||
serviceDesc.method.forEach((methodDesc) => { | ||
assertInstanceOf(methodDesc, FormattedMethodDescriptor); | ||
const httpRule = httpRules[methodDesc.formattedName]; | ||
|
||
if (!httpRule) { | ||
return; | ||
} | ||
|
||
chunks.push(code`\ | ||
${methodDesc.formattedName}: { | ||
path: "${httpRule.path}", | ||
method: "${httpRule.method}",${httpRule.body ? `\nbody: "${httpRule.body}",` : ""} | ||
requestType: undefined as unknown as ${requestType(ctx, methodDesc)}, | ||
responseType: undefined as unknown as ${responseType(ctx, methodDesc)}, | ||
},`); | ||
}); | ||
|
||
chunks.push(code`}`); | ||
return joinCode(chunks, { on: "\n" }); | ||
} | ||
|
||
return code``; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters