Comparing version 0.0.4 to 0.0.5
@@ -1,30 +0,7 @@ | ||
import { HttpAgent } from '@dfinity/agent'; | ||
export interface Canister { | ||
call(method: string, ...args: any[]): Promise<any>; | ||
} | ||
declare class DevCanister implements Canister { | ||
readonly alias: string; | ||
readonly host: string; | ||
constructor(alias: string, host: string); | ||
call(method: string, ...args: any[]): Promise<any>; | ||
} | ||
declare class ReplicaCanister implements Canister { | ||
readonly id: string; | ||
readonly agent: HttpAgent; | ||
private _actor; | ||
constructor(id: string, agent: HttpAgent); | ||
private _fetchActor; | ||
call(method: string, ...args: any[]): Promise<any>; | ||
} | ||
declare type Mocks = Record<string, (...args: any[]) => Promise<any>>; | ||
declare class MockCanister implements Canister { | ||
readonly mocks: Mocks; | ||
readonly fallback: Canister | undefined; | ||
constructor(mocks: Mocks, fallback: Canister | undefined); | ||
call(method: string, ...args: any[]): Promise<any>; | ||
} | ||
export declare function devCanister(alias: string, host?: string): DevCanister; | ||
export declare function replicaCanister(id: string, agent?: HttpAgent | undefined): ReplicaCanister; | ||
export declare function mockCanister(mocks: Mocks, parent?: Canister): MockCanister; | ||
import { replicaCanister } from './canister/replica'; | ||
import { devCanister } from './canister/dev'; | ||
import { mockCanister } from './canister/mock'; | ||
export { Canister } from './canister'; | ||
export { replicaCanister, devCanister, mockCanister }; | ||
export default replicaCanister; | ||
//# sourceMappingURL=index.d.ts.map |
131
lib/index.js
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mockCanister = exports.replicaCanister = exports.devCanister = void 0; | ||
const agent_1 = require("@dfinity/agent"); | ||
const cross_fetch_1 = __importDefault(require("cross-fetch")); | ||
class DevCanister { | ||
constructor(alias, host) { | ||
this.alias = alias; | ||
this.host = host; | ||
} | ||
call(method, ...args) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const response = yield (0, cross_fetch_1.default)(new URL(`/call/${this.alias}/${method}`, this.host), { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
args, | ||
}), | ||
}); | ||
if (!response.ok) { | ||
throw new Error(`Error while calling ${this.alias}.${method}(${args | ||
.map((a) => typeof a) | ||
.join(', ')}): ${(yield response.text()) || | ||
response.statusText || | ||
`status code ${response.status}`}`); | ||
} | ||
const body = yield response.json(); | ||
return body === null || body === void 0 ? void 0 : body.value; | ||
}); | ||
} | ||
} | ||
class ReplicaCanister { | ||
constructor(id, agent) { | ||
this.id = id; | ||
this.agent = agent; | ||
} | ||
_fetchActor() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this._actor) { | ||
return this._actor; | ||
} | ||
const source = yield (0, agent_1.fetchCandid)(this.id, this.agent); | ||
const didJsCanisterId = 'ryjl3-tyaaa-aaaaa-aaaba-cai'; | ||
const didJsInterface = ({ IDL }) => IDL.Service({ | ||
did_to_js: IDL.Func([IDL.Text], [IDL.Opt(IDL.Text)], ['query']), | ||
}); | ||
const didJs = agent_1.Actor.createActor(didJsInterface, { | ||
canisterId: didJsCanisterId, | ||
agent: this.agent, | ||
}); | ||
const js = (yield didJs.did_to_js(source))[0]; | ||
const candid = yield eval(`import("data:text/javascript;charset=utf-8,${encodeURIComponent(js)}")`); | ||
const actor = agent_1.Actor.createActor(candid.idlFactory, { | ||
agent: this.agent, | ||
canisterId: this.id, | ||
}); | ||
this._actor = actor; | ||
return actor; | ||
}); | ||
} | ||
call(method, ...args) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const actor = yield this._fetchActor(); | ||
const result = yield actor[method](...args); | ||
// Convert to JSON-like object | ||
return JSON.parse(JSON.stringify(result, (_key, value) => { | ||
if (typeof value === 'bigint') { | ||
return value.toString(); | ||
} | ||
// TODO: Principal, Blob, etc. | ||
return value; | ||
})); | ||
}); | ||
} | ||
} | ||
class MockCanister { | ||
constructor(mocks, fallback) { | ||
this.mocks = mocks; | ||
this.fallback = fallback; | ||
} | ||
call(method, ...args) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this.mocks.hasOwnProperty(method)) { | ||
return this.mocks[method].apply(this, args); | ||
} | ||
if (this.fallback) { | ||
return this.fallback.call(method, ...args); | ||
} | ||
throw new Error(`Unmocked canister method: \`${method}\``); | ||
}); | ||
} | ||
} | ||
function devCanister(alias, host = 'http://localhost:7700') { | ||
return new DevCanister(alias, host); | ||
} | ||
exports.devCanister = devCanister; | ||
function replicaCanister(id, agent = undefined) { | ||
if (!agent) { | ||
agent = new agent_1.HttpAgent(); | ||
if (agent.isLocal()) { | ||
agent.fetchRootKey(); | ||
} | ||
} | ||
return new ReplicaCanister(id, agent); | ||
} | ||
exports.replicaCanister = replicaCanister; | ||
function mockCanister(mocks, parent) { | ||
return new MockCanister(mocks, parent); | ||
} | ||
exports.mockCanister = mockCanister; | ||
exports.default = replicaCanister; | ||
exports.mockCanister = exports.devCanister = exports.replicaCanister = void 0; | ||
const replica_1 = require("./canister/replica"); | ||
Object.defineProperty(exports, "replicaCanister", { enumerable: true, get: function () { return replica_1.replicaCanister; } }); | ||
const dev_1 = require("./canister/dev"); | ||
Object.defineProperty(exports, "devCanister", { enumerable: true, get: function () { return dev_1.devCanister; } }); | ||
const mock_1 = require("./canister/mock"); | ||
Object.defineProperty(exports, "mockCanister", { enumerable: true, get: function () { return mock_1.mockCanister; } }); | ||
exports.default = replica_1.replicaCanister; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "ic0", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "An easy-to-use JavaScript API for the Internet Computer.", | ||
@@ -5,0 +5,0 @@ "author": "DFINITY Foundation", |
152
src/index.ts
@@ -1,149 +0,7 @@ | ||
import { Actor, ActorSubclass, HttpAgent, fetchCandid } from '@dfinity/agent'; | ||
import { IDL } from '@dfinity/candid'; | ||
import fetch from 'cross-fetch'; | ||
import { replicaCanister } from './canister/replica'; | ||
import { devCanister } from './canister/dev'; | ||
import { mockCanister } from './canister/mock'; | ||
export interface Canister { | ||
call(method: string, ...args: any[]): Promise<any>; | ||
} | ||
class DevCanister implements Canister { | ||
public readonly alias: string; | ||
public readonly host: string; | ||
constructor(alias: string, host: string) { | ||
this.alias = alias; | ||
this.host = host; | ||
} | ||
async call(method: string, ...args: any[]): Promise<any> { | ||
const response = await fetch( | ||
new URL(`/call/${this.alias}/${method}`, this.host), | ||
{ | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
args, | ||
}), | ||
}, | ||
); | ||
if (!response.ok) { | ||
throw new Error( | ||
`Error while calling ${this.alias}.${method}(${args | ||
.map((a) => typeof a) | ||
.join(', ')}): ${ | ||
(await response.text()) || | ||
response.statusText || | ||
`status code ${response.status}` | ||
}`, | ||
); | ||
} | ||
const body = await response.json(); | ||
return body?.value; | ||
} | ||
} | ||
class ReplicaCanister implements Canister { | ||
public readonly id: string; | ||
public readonly agent: HttpAgent; | ||
private _actor: ActorSubclass | undefined; | ||
constructor(id: string, agent: HttpAgent) { | ||
this.id = id; | ||
this.agent = agent; | ||
} | ||
private async _fetchActor() { | ||
if (this._actor) { | ||
return this._actor; | ||
} | ||
const source = await fetchCandid(this.id, this.agent); | ||
const didJsCanisterId = 'ryjl3-tyaaa-aaaaa-aaaba-cai'; | ||
const didJsInterface: IDL.InterfaceFactory = ({ IDL }) => | ||
IDL.Service({ | ||
did_to_js: IDL.Func([IDL.Text], [IDL.Opt(IDL.Text)], ['query']), | ||
}); | ||
const didJs: ActorSubclass = Actor.createActor(didJsInterface, { | ||
canisterId: didJsCanisterId, | ||
agent: this.agent, | ||
}); | ||
const js = ((await didJs.did_to_js(source)) as [string])[0]; | ||
const candid = await eval( | ||
`import("data:text/javascript;charset=utf-8,${encodeURIComponent( | ||
js, | ||
)}")`, | ||
); | ||
const actor = Actor.createActor(candid.idlFactory, { | ||
agent: this.agent, | ||
canisterId: this.id, | ||
}); | ||
this._actor = actor; | ||
return actor; | ||
} | ||
async call(method: string, ...args: any[]): Promise<any> { | ||
const actor = await this._fetchActor(); | ||
const result = await actor[method](...args); | ||
// Convert to JSON-like object | ||
return JSON.parse( | ||
JSON.stringify(result, (_key, value) => { | ||
if (typeof value === 'bigint') { | ||
return value.toString(); | ||
} | ||
// TODO: Principal, Blob, etc. | ||
return value; | ||
}), | ||
); | ||
} | ||
} | ||
type Mocks = Record<string, (...args: any[]) => Promise<any>>; | ||
class MockCanister implements Canister { | ||
public readonly mocks: Mocks; | ||
public readonly fallback: Canister | undefined; | ||
constructor(mocks: Mocks, fallback: Canister | undefined) { | ||
this.mocks = mocks; | ||
this.fallback = fallback; | ||
} | ||
async call(method: string, ...args: any[]): Promise<any> { | ||
if (this.mocks.hasOwnProperty(method)) { | ||
return this.mocks[method].apply(this, args); | ||
} | ||
if (this.fallback) { | ||
return this.fallback.call(method, ...args); | ||
} | ||
throw new Error(`Unmocked canister method: \`${method}\``); | ||
} | ||
} | ||
export function devCanister( | ||
alias: string, | ||
host = 'http://localhost:7700', | ||
): DevCanister { | ||
return new DevCanister(alias, host); | ||
} | ||
export function replicaCanister( | ||
id: string, | ||
agent: HttpAgent | undefined = undefined, | ||
): ReplicaCanister { | ||
if (!agent) { | ||
agent = new HttpAgent(); | ||
if (agent.isLocal()) { | ||
agent.fetchRootKey(); | ||
} | ||
} | ||
return new ReplicaCanister(id, agent); | ||
} | ||
export function mockCanister(mocks: Mocks, parent?: Canister) { | ||
return new MockCanister(mocks, parent); | ||
} | ||
export { Canister } from './canister'; | ||
export { replicaCanister, devCanister, mockCanister }; | ||
export default replicaCanister; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
37388
28
334
3