Skip to content

Commit aaee7d9

Browse files
committed
Use new style imports in generated TypeScript
Address lint Do not export main virtual object
1 parent 4c56e98 commit aaee7d9

7 files changed

Lines changed: 78 additions & 46 deletions

File tree

cardano-wasm/lib-wrapper/cardano-api.d.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
// cardano-api.d.ts
22

3-
/// <reference path="./unsigned-tx.d.ts" />
3+
import UnsignedTx from './unsigned-tx';
44

5-
/// <reference path="./signed-tx.d.ts" />
5+
import GrpcConnection from './grpc-connection';
66

7-
/// <reference path="./grpc-connection.d.ts" />
8-
9-
/// <reference path="./wallet.d.ts" />
10-
11-
export default initialise;
12-
13-
/**
14-
* Initialises the Cardano API.
15-
* @returns A promise that resolves to the main `CardanoApi` object.
16-
*/
17-
declare function initialise(): Promise<CardanoApi>;
7+
import Wallet from './wallet';
188

199
/**
2010
* The main Cardano API object with static methods.
@@ -66,3 +56,11 @@ declare interface CardanoApi {
6656
*/
6757
restoreTestnetPaymentWalletFromSigningKeyBech32(networkMagic: number, signingKeyBech32: string): Promise<Wallet>;
6858
}
59+
60+
/**
61+
* Initialises the Cardano API.
62+
* @returns A promise that resolves to the main `CardanoApi` object.
63+
*/
64+
declare function initialise(): Promise<CardanoApi>;
65+
66+
export default initialise;

cardano-wasm/lib-wrapper/grpc-connection.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ declare interface GrpcConnection {
4141
*/
4242
getUtxosForAddress(address: string): Promise<{ txId: string, txIndex: number, lovelace: bigint, assets: any[], datum?: any, script?: any }[]>;
4343
}
44+
45+
export default GrpcConnection;

cardano-wasm/lib-wrapper/signed-tx.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ declare interface SignedTx {
2222
*/
2323
txToCbor(): Promise<string>;
2424
}
25+
26+
export default SignedTx;

cardano-wasm/lib-wrapper/unsigned-tx.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// unsigned-tx.d.ts
22

3+
import SignedTx from './signed-tx';
4+
35
/**
46
* Represents an unsigned transaction.
57
*/
@@ -49,3 +51,5 @@ declare interface UnsignedTx {
4951
*/
5052
signWithPaymentKey(signingKey: string): Promise<SignedTx>;
5153
}
54+
55+
export default UnsignedTx;

cardano-wasm/lib-wrapper/wallet.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ declare interface Wallet {
3333
*/
3434
getBase16ForVerificationKeyHash(): Promise<string>;
3535
}
36+
37+
export default Wallet;

cardano-wasm/src/Cardano/Wasm/Internal/Api/InfoToTypeScript.hs

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@ import Cardano.Wasm.Internal.Api.Info (tsTypeAsString)
44
import Cardano.Wasm.Internal.Api.Info qualified as Info
55
import Cardano.Wasm.Internal.Api.TypeScriptDefs qualified as TypeScript
66

7+
import Data.List (nub)
8+
import Data.Map (Map)
9+
import Data.Map qualified as Map
10+
711
-- | Converts the Cardano API information to a TypeScript declaration file AST.
812
apiInfoToTypeScriptFile :: Info.ApiInfo -> [TypeScript.TypeScriptFile]
913
apiInfoToTypeScriptFile apiInfo =
1014
( TypeScript.TypeScriptFile
1115
{ TypeScript.typeScriptFileName = Info.dashCaseName (Info.mainObject apiInfo) <> ".d.ts"
1216
, TypeScript.typeScriptFileContent =
13-
map importDeclaration virtualObjectInterfaces
14-
++ [ TypeScript.Declaration [] (TypeScript.ExportDec True "initialise")
15-
, TypeScript.Declaration
17+
virtualObjectInfoToInterfaceDecs
18+
False
19+
voMap
20+
(Info.mainObject apiInfo)
21+
++ [ TypeScript.Declaration
1622
[ Info.initialiseFunctionDoc apiInfo
1723
, "@returns " <> Info.initialiseFunctionReturnDoc apiInfo
1824
]
@@ -24,47 +30,65 @@ apiInfoToTypeScriptFile apiInfo =
2430
"Promise<" <> Info.virtualObjectName (Info.mainObject apiInfo) <> ">"
2531
}
2632
)
27-
]
28-
++ [ virtualObjectInfoToInterfaceDec $ Info.mainObject apiInfo
33+
, TypeScript.Declaration [] (TypeScript.ExportDec True "initialise")
2934
]
3035
}
3136
)
3237
: virtualObjectInterfaces
3338
where
3439
virtualObjectInterfaces =
35-
map virtualObjectInfoToTypeScriptFile (Info.virtualObjects apiInfo)
40+
map (virtualObjectInfoToTypeScriptFile voMap) (Info.virtualObjects apiInfo)
41+
42+
voMap = Map.fromList [(Info.virtualObjectName vo, vo) | vo <- Info.virtualObjects apiInfo]
3643

37-
importDeclaration :: TypeScript.TypeScriptFile -> TypeScript.Declaration
38-
importDeclaration file =
44+
importDeclaration :: Info.VirtualObjectInfo -> TypeScript.Declaration
45+
importDeclaration vo =
3946
TypeScript.Declaration
4047
{ TypeScript.declarationComment = []
41-
, TypeScript.declarationContent = TypeScript.ImportDec $ "./" <> TypeScript.typeScriptFileName file
48+
, TypeScript.declarationContent =
49+
TypeScript.ImportDec (Info.virtualObjectName vo) $ Info.dashCaseName vo
4250
}
4351

44-
virtualObjectInfoToTypeScriptFile :: Info.VirtualObjectInfo -> TypeScript.TypeScriptFile
45-
virtualObjectInfoToTypeScriptFile vo =
52+
importDeclarations
53+
:: Map String Info.VirtualObjectInfo -> Info.VirtualObjectInfo -> [TypeScript.Declaration]
54+
importDeclarations voMap (Info.VirtualObjectInfo{Info.virtualObjectMethods = methods}) =
55+
map
56+
importDeclaration
57+
$ nub
58+
[ vo
59+
| Info.MethodInfo{Info.methodReturnType = Info.NewObject returnType} <- methods
60+
, Just vo <- [Map.lookup returnType voMap]
61+
]
62+
63+
virtualObjectInfoToTypeScriptFile
64+
:: Map String Info.VirtualObjectInfo -> Info.VirtualObjectInfo -> TypeScript.TypeScriptFile
65+
virtualObjectInfoToTypeScriptFile voMap vo =
4666
TypeScript.TypeScriptFile
4767
{ TypeScript.typeScriptFileName = Info.dashCaseName vo <> ".d.ts"
48-
, TypeScript.typeScriptFileContent =
49-
[virtualObjectInfoToInterfaceDec vo]
68+
, TypeScript.typeScriptFileContent = virtualObjectInfoToInterfaceDecs True voMap vo
5069
}
5170

52-
virtualObjectInfoToInterfaceDec :: Info.VirtualObjectInfo -> TypeScript.Declaration
53-
virtualObjectInfoToInterfaceDec vo =
54-
TypeScript.Declaration
55-
[Info.virtualObjectDoc vo]
56-
( TypeScript.InterfaceDec
57-
(Info.virtualObjectName vo)
58-
( [ TypeScript.InterfaceContent
59-
[ "The type of the object, used for identification (the \""
60-
<> Info.virtualObjectName vo
61-
<> "\" string)."
62-
]
63-
(TypeScript.InterfaceProperty "objectType" "string")
64-
]
65-
<> map (methodInfoToInterfaceContent (Info.virtualObjectName vo)) (Info.virtualObjectMethods vo)
66-
)
67-
)
71+
virtualObjectInfoToInterfaceDecs
72+
:: Bool -> Map String Info.VirtualObjectInfo -> Info.VirtualObjectInfo -> [TypeScript.Declaration]
73+
virtualObjectInfoToInterfaceDecs isDefaultExport voMap vo =
74+
importDeclarations voMap vo
75+
++ [ TypeScript.Declaration
76+
[Info.virtualObjectDoc vo]
77+
( TypeScript.InterfaceDec
78+
(Info.virtualObjectName vo)
79+
( [ TypeScript.InterfaceContent
80+
[ "The type of the object, used for identification (the \""
81+
<> Info.virtualObjectName vo
82+
<> "\" string)."
83+
]
84+
(TypeScript.InterfaceProperty "objectType" "string")
85+
]
86+
<> map (methodInfoToInterfaceContent (Info.virtualObjectName vo)) (Info.virtualObjectMethods vo)
87+
)
88+
)
89+
]
90+
++ [ TypeScript.Declaration [] (TypeScript.ExportDec True $ Info.virtualObjectName vo) | isDefaultExport
91+
]
6892

6993
methodInfoToInterfaceContent :: String -> Info.MethodInfo -> TypeScript.InterfaceContent
7094
methodInfoToInterfaceContent selfTypeName method =

cardano-wasm/src/Cardano/Wasm/Internal/Api/TypeScriptDefs.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ data DeclarationType
9393
-- ^ Definitions of the interface.
9494
| -- | Reference to import another TypeScript declaration file.
9595
ImportDec
96+
String
97+
-- ^ Name of the symbol to import.
9698
String
9799
-- ^ Path to the TypeScript declaration file to import.
98100

@@ -111,10 +113,8 @@ buildDeclarationType (InterfaceDec name properties) =
111113
<> " {"
112114
<> mconcat (map (\prop -> "\n" <> buildInterfaceContent prop <> "\n") properties)
113115
<> "}"
114-
buildDeclarationType (ImportDec path) =
115-
"/// <reference path=\""
116-
<> TLB.fromString path
117-
<> "\" />"
116+
buildDeclarationType (ImportDec symbolName path) =
117+
"import " <> TLB.fromString symbolName <> " from './" <> TLB.fromString path <> "';"
118118

119119
-- | Represents a function parameter in TypeScript.
120120
data FunctionParam = FunctionParam

0 commit comments

Comments
 (0)