@@ -37,19 +37,21 @@ export interface ActorAccessor<AD extends AnyActorDefinition> {
37
37
* The actor name is automatically injected from the property accessor.
38
38
*
39
39
* @template A The actor class that this handle is connected to.
40
- * @param {Omit<GetOptions, 'name'> } [opts] - Options for getting the actor.
40
+ * @param {ActorTags } [tags={}] - The tags to identify the actor. Defaults to an empty object.
41
+ * @param {GetOptions } [opts] - Options for getting the actor.
41
42
* @returns {Promise<ActorHandle<AD>> } - A promise resolving to the actor handle.
42
43
*/
43
- get ( opts ?: Omit < GetOptions , "name" > ) : Promise < ActorHandle < AD > > ;
44
+ get ( tags ?: ActorTags , opts ?: GetOptions ) : Promise < ActorHandle < AD > > ;
44
45
45
46
/**
46
47
* Creates a new actor with the name automatically injected from the property accessor.
47
48
*
48
49
* @template A The actor class that this handle is connected to.
49
- * @param {Omit<CreateOptions, 'name'> } opts - Options for creating the actor.
50
+ * @param {CreateOptions } opts - Options for creating the actor (excluding name and tags).
51
+ * @param {ActorTags } [tags={}] - The tags to identify the actor. Defaults to an empty object.
50
52
* @returns {Promise<ActorHandle<AD>> } - A promise resolving to the actor handle.
51
53
*/
52
- create ( opts : Omit < CreateOptions , "name" > ) : Promise < ActorHandle < AD > > ;
54
+ create ( opts : CreateOptions , tags ?: ActorTags ) : Promise < ActorHandle < AD > > ;
53
55
54
56
/**
55
57
* Gets an actor by its ID.
@@ -94,7 +96,6 @@ export interface GetWithIdOptions extends QueryOptions {}
94
96
* @property {Partial<CreateRequest> } [create] - Config used to create the actor.
95
97
*/
96
98
export interface GetOptions extends QueryOptions {
97
- tags ?: ActorTags ;
98
99
/** Prevents creating a new actor if one does not exist. */
99
100
noCreate ?: boolean ;
100
101
/** Config used to create the actor. */
@@ -104,12 +105,9 @@ export interface GetOptions extends QueryOptions {
104
105
/**
105
106
* Options for creating an actor.
106
107
* @typedef {QueryOptions } CreateOptions
107
- * @property {CreateRequest } create - Config used to create the actor .
108
+ * @property {Object } - Additional options for actor creation excluding name and tags that come from the tags parameter .
108
109
*/
109
- export interface CreateOptions extends QueryOptions {
110
- /** Config used to create the actor. */
111
- create : Omit < CreateRequest , "name" > ;
112
- }
110
+ export interface CreateOptions extends QueryOptions , Omit < CreateRequest , "name" | "tags" > { }
113
111
114
112
/**
115
113
* Represents a region to connect to.
@@ -147,19 +145,21 @@ export interface ActorAccessor<AD extends AnyActorDefinition> {
147
145
* The actor name is automatically injected from the property accessor.
148
146
*
149
147
* @template A The actor class that this handle is connected to.
150
- * @param {Omit<GetOptions, 'name'> } [opts] - Options for getting the actor.
148
+ * @param {ActorTags } [tags={}] - The tags to identify the actor. Defaults to an empty object.
149
+ * @param {GetOptions } [opts] - Options for getting the actor.
151
150
* @returns {Promise<ActorHandle<AD>> } - A promise resolving to the actor handle.
152
151
*/
153
- get ( opts ?: GetOptions ) : Promise < ActorHandle < AD > > ;
152
+ get ( tags ?: ActorTags , opts ?: GetOptions ) : Promise < ActorHandle < AD > > ;
154
153
155
154
/**
156
155
* Creates a new actor with the name automatically injected from the property accessor.
157
156
*
158
157
* @template A The actor class that this handle is connected to.
159
- * @param {Omit<CreateOptions, 'name'> } opts - Options for creating the actor.
158
+ * @param {CreateOptions } opts - Options for creating the actor (excluding name and tags).
159
+ * @param {ActorTags } [tags={}] - The tags to identify the actor. Defaults to an empty object.
160
160
* @returns {Promise<ActorHandle<AD>> } - A promise resolving to the actor handle.
161
161
*/
162
- create ( opts : CreateOptions ) : Promise < ActorHandle < AD > > ;
162
+ create ( opts : CreateOptions , tags ?: ActorTags ) : Promise < ActorHandle < AD > > ;
163
163
164
164
/**
165
165
* Gets an actor by its ID.
@@ -268,41 +268,40 @@ export class ClientRaw {
268
268
*
269
269
* @example
270
270
* ```
271
- * const room = await client.get<ChatRoom>({
272
- * name: 'chat_room',
271
+ * const room = await client.get<ChatRoom>(
273
272
* // Get or create the actor for the channel `random`
274
- * channel: 'random'
275
- * } );
273
+ * { name: 'my_document', channel: 'random' },
274
+ * );
276
275
*
277
- * // This actor will have the tags: { name: 'chat_room ', channel: 'random' }
276
+ * // This actor will have the tags: { name: 'my_document ', channel: 'random' }
278
277
* await room.sendMessage('Hello, world!');
279
278
* ```
280
279
*
281
280
* @template AD The actor class that this handle is connected to.
282
- * @param {ActorTags } tags - The tags to identify the actor.
281
+ * @param {ActorTags } [ tags={}] - The tags to identify the actor. Defaults to an empty object .
283
282
* @param {GetOptions } [opts] - Options for getting the actor.
284
283
* @returns {Promise<ActorHandle<AD>> } - A promise resolving to the actor handle.
285
284
* @see {@link https://rivet.gg/docs/manage#client.get }
286
285
*/
287
286
async get < AD extends AnyActorDefinition > (
288
- name : string ,
287
+ tags : ActorTags = { } ,
289
288
opts ?: GetOptions ,
290
289
) : Promise < ActorHandle < AD > > {
291
- let tags = opts ?. tags ?? { } ;
290
+ // Extract name from tags
291
+ const { name, ...restTags } = tags ;
292
292
293
293
// Build create config
294
294
let create : CreateRequest | undefined = undefined ;
295
295
if ( ! opts ?. noCreate ) {
296
296
create = {
297
297
name,
298
298
// Fall back to tags defined when querying actor
299
- tags : opts ?. create ?. tags ?? tags ,
299
+ tags : opts ?. create ?. tags ?? restTags ,
300
300
...opts ?. create ,
301
301
} ;
302
302
}
303
303
304
304
logger ( ) . debug ( "get actor" , {
305
- name,
306
305
tags,
307
306
parameters : opts ?. params ,
308
307
create,
@@ -315,7 +314,7 @@ export class ClientRaw {
315
314
query : {
316
315
getOrCreateForTags : {
317
316
name,
318
- tags,
317
+ tags : restTags ,
319
318
create,
320
319
} ,
321
320
} ,
@@ -335,38 +334,39 @@ export class ClientRaw {
335
334
* @example
336
335
* ```
337
336
* // Create a new document actor
338
- * const doc = await client.create<MyDocument>({
339
- * create: {
340
- * tags: {
341
- * name: 'my_document',
342
- * docId: '123'
343
- * }
344
- * }
345
- * });
337
+ * const doc = await client.create<MyDocument>(
338
+ * { region: 'us-east-1' },
339
+ * { name: 'my_document', docId: '123' }
340
+ * );
346
341
*
347
342
* await doc.doSomething();
348
343
* ```
349
344
*
350
345
* @template AD The actor class that this handle is connected to.
351
- * @param {CreateOptions } opts - Options for creating the actor.
346
+ * @param {CreateOptions } opts - Options for creating the actor (excluding name and tags).
347
+ * @param {ActorTags } [tags={}] - The tags to identify the actor. Defaults to an empty object.
352
348
* @returns {Promise<ActorHandle<AD>> } - A promise resolving to the actor handle.
353
349
* @see {@link https://rivet.gg/docs/manage#client.create }
354
350
*/
355
351
async create < AD extends AnyActorDefinition > (
356
- name : string ,
357
352
opts : CreateOptions ,
353
+ tags : ActorTags = { } ,
358
354
) : Promise < ActorHandle < AD > > {
355
+ // Extract name from tags
356
+ const { name, ...restTags } = tags ;
357
+
359
358
// Build create config
360
359
const create = {
361
360
name,
362
- ...opts . create ,
361
+ tags : restTags ,
362
+ ...opts ,
363
363
} ;
364
364
365
365
// Default to the chosen region
366
366
//if (!create.region) create.region = (await this.#regionPromise)?.id;
367
367
368
368
logger ( ) . debug ( "create actor" , {
369
- name ,
369
+ tags ,
370
370
parameters : opts ?. params ,
371
371
create,
372
372
} ) ;
@@ -642,16 +642,21 @@ export function createClient<A extends ActorCoreApp<any>>(
642
642
// Return actor accessor object with methods
643
643
return {
644
644
get : (
645
+ tags ?: ActorTags ,
645
646
opts ?: GetOptions ,
646
647
) : Promise < ActorHandle < ExtractActorsFromApp < A > [ typeof prop ] > > => {
647
- return target . get < ExtractActorsFromApp < A > [ typeof prop ] > ( prop , opts ) ;
648
+ return target . get < ExtractActorsFromApp < A > [ typeof prop ] > (
649
+ { name : prop , ...( tags || { } ) } ,
650
+ opts
651
+ ) ;
648
652
} ,
649
653
create : (
650
654
opts : CreateOptions ,
655
+ tags ?: ActorTags ,
651
656
) : Promise < ActorHandle < ExtractActorsFromApp < A > [ typeof prop ] > > => {
652
657
return target . create < ExtractActorsFromApp < A > [ typeof prop ] > (
653
- prop ,
654
658
opts ,
659
+ { name : prop , ...( tags || { } ) }
655
660
) ;
656
661
} ,
657
662
getWithId : (
@@ -669,4 +674,4 @@ export function createClient<A extends ActorCoreApp<any>>(
669
674
return undefined ;
670
675
} ,
671
676
} ) as Client < A > ;
672
- }
677
+ }
0 commit comments