Skip to content

Commit 9a11fca

Browse files
Merge pull request #1128 from dfinity/raymond/SDK-1609-superheroes
chore: use standard port for superheroes example
2 parents 0157cb7 + 6be76df commit 9a11fca

File tree

13 files changed

+244
-449
lines changed

13 files changed

+244
-449
lines changed

motoko/superheroes/package-lock.json

Lines changed: 135 additions & 396 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

motoko/superheroes/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"typescript": "^4.3.5",
4848
"util": "0.12.3",
4949
"webpack": "^5.87.0",
50-
"webpack-cli": "4.5.0",
50+
"webpack-cli": "^4.10.0",
5151
"webpack-dev-server": "^4.6.0"
5252
},
5353
"browserslist": [

motoko/superheroes/src/declarations/index.js

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type {
2+
ActorSubclass,
3+
HttpAgentOptions,
4+
ActorConfig,
5+
Agent,
6+
} from "@dfinity/agent";
7+
import type { Principal } from "@dfinity/principal";
8+
import type { IDL } from "@dfinity/candid";
9+
10+
import { _SERVICE } from './superheroes.did';
11+
12+
export declare const idlFactory: IDL.InterfaceFactory;
13+
export declare const canisterId: string;
14+
15+
export declare interface CreateActorOptions {
16+
/**
17+
* @see {@link Agent}
18+
*/
19+
agent?: Agent;
20+
/**
21+
* @see {@link HttpAgentOptions}
22+
*/
23+
agentOptions?: HttpAgentOptions;
24+
/**
25+
* @see {@link ActorConfig}
26+
*/
27+
actorOptions?: ActorConfig;
28+
}
29+
30+
/**
31+
* Intializes an {@link ActorSubclass}, configured with the provided SERVICE interface of a canister.
32+
* @constructs {@link ActorSubClass}
33+
* @param {string | Principal} canisterId - ID of the canister the {@link Actor} will talk to
34+
* @param {CreateActorOptions} options - see {@link CreateActorOptions}
35+
* @param {CreateActorOptions["agent"]} options.agent - a pre-configured agent you'd like to use. Supercedes agentOptions
36+
* @param {CreateActorOptions["agentOptions"]} options.agentOptions - options to set up a new agent
37+
* @see {@link HttpAgentOptions}
38+
* @param {CreateActorOptions["actorOptions"]} options.actorOptions - options for the Actor
39+
* @see {@link ActorConfig}
40+
*/
41+
export declare const createActor: (
42+
canisterId: string | Principal,
43+
options?: CreateActorOptions
44+
) => ActorSubclass<_SERVICE>;
45+
46+
/**
47+
* Intialized Actor using default settings, ready to talk to a canister using its candid interface
48+
* @constructs {@link ActorSubClass}
49+
*/
50+
export declare const superheroes: ActorSubclass<_SERVICE>;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Actor, HttpAgent } from "@dfinity/agent";
2+
3+
// Imports and re-exports candid interface
4+
import { idlFactory } from "./superheroes.did.js";
5+
export { idlFactory } from "./superheroes.did.js";
6+
7+
/* CANISTER_ID is replaced by webpack based on node environment
8+
* Note: canister environment variable will be standardized as
9+
* process.env.CANISTER_ID_<CANISTER_NAME_UPPERCASE>
10+
* beginning in dfx 0.15.0
11+
*/
12+
export const canisterId =
13+
process.env.CANISTER_ID_SUPERHEROES;
14+
15+
export const createActor = (canisterId, options = {}) => {
16+
const agent = options.agent || new HttpAgent({ ...options.agentOptions });
17+
18+
if (options.agent && options.agentOptions) {
19+
console.warn(
20+
"Detected both agent and agentOptions passed to createActor. Ignoring agentOptions and proceeding with the provided agent."
21+
);
22+
}
23+
24+
// Fetch root key for certificate validation during development
25+
if (process.env.DFX_NETWORK !== "ic") {
26+
agent.fetchRootKey().catch((err) => {
27+
console.warn(
28+
"Unable to fetch root key. Check to ensure that your local replica is running"
29+
);
30+
console.error(err);
31+
});
32+
}
33+
34+
// Creates an actor with using the candid interface and the HttpAgent
35+
return Actor.createActor(idlFactory, {
36+
agent,
37+
canisterId,
38+
...options.actorOptions,
39+
});
40+
};
41+
42+
export const superheroes = canisterId ? createActor(canisterId) : undefined;

motoko/superheroes/src/declarations/superheroes.did renamed to motoko/superheroes/src/declarations/superheroes/superheroes.did

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type List =
1010
List;
1111
};
1212
service : {
13+
/// * High-Level API
1314
create: (Superhero) -> (SuperheroId);
1415
delete: (SuperheroId) -> (bool);
1516
read: (SuperheroId) -> (opt Superhero) query;

motoko/superheroes/src/declarations/superheroes.did.d.ts renamed to motoko/superheroes/src/declarations/superheroes/superheroes.did.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Principal } from '@dfinity/principal';
22
import type { ActorMethod } from '@dfinity/agent';
3+
import type { IDL } from '@dfinity/candid';
34

45
export type List = [] | [[string, List]];
56
export interface Superhero { 'superpowers' : List, 'name' : string }
@@ -10,3 +11,5 @@ export interface _SERVICE {
1011
'read' : ActorMethod<[SuperheroId], [] | [Superhero]>,
1112
'update' : ActorMethod<[SuperheroId, Superhero], boolean>,
1213
}
14+
export declare const idlFactory: IDL.InterfaceFactory;
15+
export declare const init: (args: { IDL: typeof IDL }) => IDL.Type[];

motoko/superheroes/src/declarations/superheroes.did.js renamed to motoko/superheroes/src/declarations/superheroes/superheroes.did.js

File renamed without changes.

motoko/superheroes/src/www/components/create.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22

3-
import { superheroes } from "../../declarations";
3+
import { superheroes } from "../../declarations/superheroes";
44

55
const $ = document.getElementById.bind(document);
66
const idl = require('../utilities/idl');

motoko/superheroes/src/www/components/delete.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22

3-
import { superheroes } from "../../declarations";
3+
import { superheroes } from "../../declarations/superheroes";
44

55
const $ = document.getElementById.bind(document);
66
const idl = require('../utilities/idl');

0 commit comments

Comments
 (0)