-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new intent struct generate typescript
- Loading branch information
Showing
2 changed files
with
363 additions
and
0 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,362 @@ | ||
/* eslint-disable */ | ||
// sequence-waas-intents v0.1.0 b0dc5c3bceb25d04f130e1807829bd8110a0a3e4 | ||
// -- | ||
// Code generated by [email protected] with typescript generator. DO NOT EDIT. | ||
// | ||
// webrpc-gen -schema=intent.ridl -target=typescript -client -out=./intent.gen.ts | ||
|
||
// WebRPC description and code-gen version | ||
export const WebRPCVersion = "v1" | ||
|
||
// Schema version of your RIDL schema | ||
export const WebRPCSchemaVersion = "v0.1.0" | ||
|
||
// Schema hash generated from your RIDL schema | ||
export const WebRPCSchemaHash = "b0dc5c3bceb25d04f130e1807829bd8110a0a3e4" | ||
|
||
// | ||
// Types | ||
// | ||
|
||
|
||
export interface Signature { | ||
sessionId: string | ||
signature: string | ||
} | ||
|
||
export interface Intent { | ||
version: string | ||
name: string | ||
expires: number | ||
issued: number | ||
data: any | ||
signatures: Array<Signature> | ||
} | ||
|
||
export interface SessionPacketProof { | ||
email?: string | ||
idToken?: string | ||
} | ||
|
||
export interface IntentDataOpenSession { | ||
sessionId: string | ||
proof: SessionPacketProof | ||
} | ||
|
||
export interface IntentDataCloseSession { | ||
sessionId: string | ||
} | ||
|
||
export interface IntentDataValidateSession { | ||
sessionId: string | ||
deviceMetadata: string | ||
} | ||
|
||
export interface IntentDataFinishValidateSession { | ||
sessionId: string | ||
salt: string | ||
challenge: string | ||
} | ||
|
||
export interface IntentDataListSessions { | ||
} | ||
|
||
export interface IntentDataGetSession { | ||
sessionId: string | ||
} | ||
|
||
export interface IntentDataSign { | ||
network: string | ||
message: string | ||
} | ||
|
||
export interface IntentDataTransaction { | ||
network: string | ||
identifier: string | ||
transactions: Array<any> | ||
} | ||
|
||
export interface TransactionRaw { | ||
type: string | ||
to: string | ||
value: string | ||
data: string | ||
} | ||
|
||
export interface TransactionERC20 { | ||
type: string | ||
token: string | ||
to: string | ||
value: string | ||
} | ||
|
||
export interface TransactionERC721 { | ||
type: string | ||
token: string | ||
to: string | ||
id: string | ||
safe?: boolean | ||
data?: string | ||
} | ||
|
||
export interface TransactionERC1155Value { | ||
id: string | ||
amount: string | ||
} | ||
|
||
export interface TransactionERC1155 { | ||
type: string | ||
token: string | ||
to: string | ||
vals: Array<TransactionERC1155Value> | ||
data?: string | ||
} | ||
|
||
export interface IntentResponse { | ||
code: string | ||
data: any | ||
} | ||
|
||
|
||
|
||
const createHTTPRequest = (body: object = {}, headers: object = {}, signal: AbortSignal | null = null): object => { | ||
return { | ||
method: 'POST', | ||
headers: { ...headers, 'Content-Type': 'application/json' }, | ||
body: JSON.stringify(body || {}), | ||
signal | ||
} | ||
} | ||
|
||
const buildResponse = (res: Response): Promise<any> => { | ||
return res.text().then(text => { | ||
let data | ||
try { | ||
data = JSON.parse(text) | ||
} catch(error) { | ||
let message = '' | ||
if (error instanceof Error) { | ||
message = error.message | ||
} | ||
throw WebrpcBadResponseError.new({ | ||
status: res.status, | ||
cause: `JSON.parse(): ${message}: response text: ${text}`}, | ||
) | ||
} | ||
if (!res.ok) { | ||
const code: number = (typeof data.code === 'number') ? data.code : 0 | ||
throw (webrpcErrorByCode[code] || WebrpcError).new(data) | ||
} | ||
return data | ||
}) | ||
} | ||
|
||
// | ||
// Errors | ||
// | ||
|
||
export class WebrpcError extends Error { | ||
name: string | ||
code: number | ||
message: string | ||
status: number | ||
cause?: string | ||
|
||
/** @deprecated Use message instead of msg. Deprecated in webrpc v0.11.0. */ | ||
msg: string | ||
|
||
constructor(name: string, code: number, message: string, status: number, cause?: string) { | ||
super(message) | ||
this.name = name || 'WebrpcError' | ||
this.code = typeof code === 'number' ? code : 0 | ||
this.message = message || `endpoint error ${this.code}` | ||
this.msg = this.message | ||
this.status = typeof status === 'number' ? status : 0 | ||
this.cause = cause | ||
Object.setPrototypeOf(this, WebrpcError.prototype) | ||
} | ||
|
||
static new(payload: any): WebrpcError { | ||
return new this(payload.error, payload.code, payload.message || payload.msg, payload.status, payload.cause) | ||
} | ||
} | ||
|
||
// Webrpc errors | ||
|
||
export class WebrpcEndpointError extends WebrpcError { | ||
constructor( | ||
name: string = 'WebrpcEndpoint', | ||
code: number = 0, | ||
message: string = 'endpoint error', | ||
status: number = 0, | ||
cause?: string | ||
) { | ||
super(name, code, message, status, cause) | ||
Object.setPrototypeOf(this, WebrpcEndpointError.prototype) | ||
} | ||
} | ||
|
||
export class WebrpcRequestFailedError extends WebrpcError { | ||
constructor( | ||
name: string = 'WebrpcRequestFailed', | ||
code: number = -1, | ||
message: string = 'request failed', | ||
status: number = 0, | ||
cause?: string | ||
) { | ||
super(name, code, message, status, cause) | ||
Object.setPrototypeOf(this, WebrpcRequestFailedError.prototype) | ||
} | ||
} | ||
|
||
export class WebrpcBadRouteError extends WebrpcError { | ||
constructor( | ||
name: string = 'WebrpcBadRoute', | ||
code: number = -2, | ||
message: string = 'bad route', | ||
status: number = 0, | ||
cause?: string | ||
) { | ||
super(name, code, message, status, cause) | ||
Object.setPrototypeOf(this, WebrpcBadRouteError.prototype) | ||
} | ||
} | ||
|
||
export class WebrpcBadMethodError extends WebrpcError { | ||
constructor( | ||
name: string = 'WebrpcBadMethod', | ||
code: number = -3, | ||
message: string = 'bad method', | ||
status: number = 0, | ||
cause?: string | ||
) { | ||
super(name, code, message, status, cause) | ||
Object.setPrototypeOf(this, WebrpcBadMethodError.prototype) | ||
} | ||
} | ||
|
||
export class WebrpcBadRequestError extends WebrpcError { | ||
constructor( | ||
name: string = 'WebrpcBadRequest', | ||
code: number = -4, | ||
message: string = 'bad request', | ||
status: number = 0, | ||
cause?: string | ||
) { | ||
super(name, code, message, status, cause) | ||
Object.setPrototypeOf(this, WebrpcBadRequestError.prototype) | ||
} | ||
} | ||
|
||
export class WebrpcBadResponseError extends WebrpcError { | ||
constructor( | ||
name: string = 'WebrpcBadResponse', | ||
code: number = -5, | ||
message: string = 'bad response', | ||
status: number = 0, | ||
cause?: string | ||
) { | ||
super(name, code, message, status, cause) | ||
Object.setPrototypeOf(this, WebrpcBadResponseError.prototype) | ||
} | ||
} | ||
|
||
export class WebrpcServerPanicError extends WebrpcError { | ||
constructor( | ||
name: string = 'WebrpcServerPanic', | ||
code: number = -6, | ||
message: string = 'server panic', | ||
status: number = 0, | ||
cause?: string | ||
) { | ||
super(name, code, message, status, cause) | ||
Object.setPrototypeOf(this, WebrpcServerPanicError.prototype) | ||
} | ||
} | ||
|
||
export class WebrpcInternalErrorError extends WebrpcError { | ||
constructor( | ||
name: string = 'WebrpcInternalError', | ||
code: number = -7, | ||
message: string = 'internal error', | ||
status: number = 0, | ||
cause?: string | ||
) { | ||
super(name, code, message, status, cause) | ||
Object.setPrototypeOf(this, WebrpcInternalErrorError.prototype) | ||
} | ||
} | ||
|
||
export class WebrpcClientDisconnectedError extends WebrpcError { | ||
constructor( | ||
name: string = 'WebrpcClientDisconnected', | ||
code: number = -8, | ||
message: string = 'client disconnected', | ||
status: number = 0, | ||
cause?: string | ||
) { | ||
super(name, code, message, status, cause) | ||
Object.setPrototypeOf(this, WebrpcClientDisconnectedError.prototype) | ||
} | ||
} | ||
|
||
export class WebrpcStreamLostError extends WebrpcError { | ||
constructor( | ||
name: string = 'WebrpcStreamLost', | ||
code: number = -9, | ||
message: string = 'stream lost', | ||
status: number = 0, | ||
cause?: string | ||
) { | ||
super(name, code, message, status, cause) | ||
Object.setPrototypeOf(this, WebrpcStreamLostError.prototype) | ||
} | ||
} | ||
|
||
export class WebrpcStreamFinishedError extends WebrpcError { | ||
constructor( | ||
name: string = 'WebrpcStreamFinished', | ||
code: number = -10, | ||
message: string = 'stream finished', | ||
status: number = 0, | ||
cause?: string | ||
) { | ||
super(name, code, message, status, cause) | ||
Object.setPrototypeOf(this, WebrpcStreamFinishedError.prototype) | ||
} | ||
} | ||
|
||
|
||
// Schema errors | ||
|
||
|
||
export enum errors { | ||
WebrpcEndpoint = 'WebrpcEndpoint', | ||
WebrpcRequestFailed = 'WebrpcRequestFailed', | ||
WebrpcBadRoute = 'WebrpcBadRoute', | ||
WebrpcBadMethod = 'WebrpcBadMethod', | ||
WebrpcBadRequest = 'WebrpcBadRequest', | ||
WebrpcBadResponse = 'WebrpcBadResponse', | ||
WebrpcServerPanic = 'WebrpcServerPanic', | ||
WebrpcInternalError = 'WebrpcInternalError', | ||
WebrpcClientDisconnected = 'WebrpcClientDisconnected', | ||
WebrpcStreamLost = 'WebrpcStreamLost', | ||
WebrpcStreamFinished = 'WebrpcStreamFinished', | ||
} | ||
|
||
const webrpcErrorByCode: { [code: number]: any } = { | ||
[0]: WebrpcEndpointError, | ||
[-1]: WebrpcRequestFailedError, | ||
[-2]: WebrpcBadRouteError, | ||
[-3]: WebrpcBadMethodError, | ||
[-4]: WebrpcBadRequestError, | ||
[-5]: WebrpcBadResponseError, | ||
[-6]: WebrpcServerPanicError, | ||
[-7]: WebrpcInternalErrorError, | ||
[-8]: WebrpcClientDisconnectedError, | ||
[-9]: WebrpcStreamLostError, | ||
[-10]: WebrpcStreamFinishedError, | ||
} | ||
|
||
export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise<Response> | ||
|
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,4 +1,5 @@ | ||
// Server | ||
//go:generate go run github.com/webrpc/webrpc/cmd/webrpc-gen -schema=intent.ridl -target=golang -pkg=intents -client -out=./intent.gen.go | ||
//go:generate go run github.com/webrpc/webrpc/cmd/webrpc-gen -schema=intent.ridl -target=typescript -client -out=./intent.gen.ts | ||
|
||
package intents |