@instantdb/admin
Advanced tools
Comparing version 0.10.4 to 0.10.5
@@ -68,2 +68,9 @@ import { tx, TransactionChunk } from "@instantdb/core"; | ||
}; | ||
declare type ImpersonationOpts = { | ||
email: string; | ||
} | { | ||
token: AuthToken; | ||
} | { | ||
guest: true; | ||
}; | ||
declare const id: any; | ||
@@ -103,4 +110,15 @@ /** | ||
auth: Auth; | ||
impersonationOpts?: ImpersonationOpts; | ||
constructor(_config: Config); | ||
/** | ||
* Sometimes you want to scope queries to a specific user. | ||
* | ||
* You can provide a user's auth token, email, or impersonate a guest. | ||
* | ||
* @see https://docs.instantdb.com/docs/backend#impersonating-users | ||
* @example | ||
* await db.asUser({email: "stopa@instantdb.com"}).query({ goals: {} }) | ||
*/ | ||
asUser: (opts: ImpersonationOpts) => InstantAdmin<Schema>; | ||
/** | ||
* Use this to query your data! | ||
@@ -107,0 +125,0 @@ * |
@@ -49,5 +49,17 @@ "use strict"; | ||
} | ||
function authorizedHeaders(config) { | ||
function withImpersonation(headers, opts) { | ||
if ("email" in opts) { | ||
headers["as-email"] = opts.email; | ||
} | ||
else if ("token" in opts) { | ||
headers["as-token"] = opts.token; | ||
} | ||
else if ("guest" in opts) { | ||
headers["as-guest"] = "true"; | ||
} | ||
return headers; | ||
} | ||
function authorizedHeaders(config, impersonationOpts) { | ||
const { adminToken, appId } = config; | ||
return { | ||
const headers = { | ||
"content-type": "application/json", | ||
@@ -57,2 +69,5 @@ authorization: `Bearer ${adminToken}`, | ||
}; | ||
return impersonationOpts | ||
? withImpersonation(headers, impersonationOpts) | ||
: headers; | ||
} | ||
@@ -104,2 +119,16 @@ function jsonFetch(input, init) { | ||
/** | ||
* Sometimes you want to scope queries to a specific user. | ||
* | ||
* You can provide a user's auth token, email, or impersonate a guest. | ||
* | ||
* @see https://docs.instantdb.com/docs/backend#impersonating-users | ||
* @example | ||
* await db.asUser({email: "stopa@instantdb.com"}).query({ goals: {} }) | ||
*/ | ||
this.asUser = (opts) => { | ||
const newClient = new InstantAdmin(Object.assign({}, this.config)); | ||
newClient.impersonationOpts = opts; | ||
return newClient; | ||
}; | ||
/** | ||
* Use this to query your data! | ||
@@ -122,3 +151,3 @@ * | ||
method: "POST", | ||
headers: authorizedHeaders(this.config), | ||
headers: authorizedHeaders(this.config, this.impersonationOpts), | ||
body: JSON.stringify({ query: query }), | ||
@@ -155,3 +184,3 @@ }); | ||
method: "POST", | ||
headers: authorizedHeaders(this.config), | ||
headers: authorizedHeaders(this.config, this.impersonationOpts), | ||
body: JSON.stringify({ steps: steps }), | ||
@@ -158,0 +187,0 @@ }); |
@@ -68,2 +68,9 @@ import { tx, TransactionChunk } from "@instantdb/core"; | ||
}; | ||
declare type ImpersonationOpts = { | ||
email: string; | ||
} | { | ||
token: AuthToken; | ||
} | { | ||
guest: true; | ||
}; | ||
declare const id: any; | ||
@@ -103,4 +110,15 @@ /** | ||
auth: Auth; | ||
impersonationOpts?: ImpersonationOpts; | ||
constructor(_config: Config); | ||
/** | ||
* Sometimes you want to scope queries to a specific user. | ||
* | ||
* You can provide a user's auth token, email, or impersonate a guest. | ||
* | ||
* @see https://docs.instantdb.com/docs/backend#impersonating-users | ||
* @example | ||
* await db.asUser({email: "stopa@instantdb.com"}).query({ goals: {} }) | ||
*/ | ||
asUser: (opts: ImpersonationOpts) => InstantAdmin<Schema>; | ||
/** | ||
* Use this to query your data! | ||
@@ -107,0 +125,0 @@ * |
@@ -21,5 +21,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
} | ||
function authorizedHeaders(config) { | ||
function withImpersonation(headers, opts) { | ||
if ("email" in opts) { | ||
headers["as-email"] = opts.email; | ||
} | ||
else if ("token" in opts) { | ||
headers["as-token"] = opts.token; | ||
} | ||
else if ("guest" in opts) { | ||
headers["as-guest"] = "true"; | ||
} | ||
return headers; | ||
} | ||
function authorizedHeaders(config, impersonationOpts) { | ||
const { adminToken, appId } = config; | ||
return { | ||
const headers = { | ||
"content-type": "application/json", | ||
@@ -29,2 +41,5 @@ authorization: `Bearer ${adminToken}`, | ||
}; | ||
return impersonationOpts | ||
? withImpersonation(headers, impersonationOpts) | ||
: headers; | ||
} | ||
@@ -75,2 +90,16 @@ function jsonFetch(input, init) { | ||
/** | ||
* Sometimes you want to scope queries to a specific user. | ||
* | ||
* You can provide a user's auth token, email, or impersonate a guest. | ||
* | ||
* @see https://docs.instantdb.com/docs/backend#impersonating-users | ||
* @example | ||
* await db.asUser({email: "stopa@instantdb.com"}).query({ goals: {} }) | ||
*/ | ||
this.asUser = (opts) => { | ||
const newClient = new InstantAdmin(Object.assign({}, this.config)); | ||
newClient.impersonationOpts = opts; | ||
return newClient; | ||
}; | ||
/** | ||
* Use this to query your data! | ||
@@ -93,3 +122,3 @@ * | ||
method: "POST", | ||
headers: authorizedHeaders(this.config), | ||
headers: authorizedHeaders(this.config, this.impersonationOpts), | ||
body: JSON.stringify({ query: query }), | ||
@@ -126,3 +155,3 @@ }); | ||
method: "POST", | ||
headers: authorizedHeaders(this.config), | ||
headers: authorizedHeaders(this.config, this.impersonationOpts), | ||
body: JSON.stringify({ steps: steps }), | ||
@@ -129,0 +158,0 @@ }); |
{ | ||
"name": "@instantdb/admin", | ||
"version": "0.10.4", | ||
"version": "0.10.5", | ||
"description": "Admin SDK for Instant DB", | ||
@@ -27,5 +27,5 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@instantdb/core": "0.10.4", | ||
"@instantdb/core": "0.10.5", | ||
"uuid": "^9.0.1" | ||
} | ||
} |
@@ -74,3 +74,2 @@ import { tx, TransactionChunk, getOps } from "@instantdb/core"; | ||
type Config = { | ||
@@ -84,2 +83,7 @@ appId: string; | ||
type ImpersonationOpts = | ||
| { email: string } | ||
| { token: AuthToken } | ||
| { guest: true }; | ||
const id = uuid.v4; | ||
@@ -96,5 +100,22 @@ | ||
function authorizedHeaders(config: FilledConfig) { | ||
function withImpersonation( | ||
headers: { [key: string]: string }, | ||
opts: ImpersonationOpts, | ||
) { | ||
if ("email" in opts) { | ||
headers["as-email"] = opts.email; | ||
} else if ("token" in opts) { | ||
headers["as-token"] = opts.token; | ||
} else if ("guest" in opts) { | ||
headers["as-guest"] = "true"; | ||
} | ||
return headers; | ||
} | ||
function authorizedHeaders( | ||
config: FilledConfig, | ||
impersonationOpts?: ImpersonationOpts, | ||
) { | ||
const { adminToken, appId } = config; | ||
return { | ||
const headers = { | ||
"content-type": "application/json", | ||
@@ -104,2 +125,5 @@ authorization: `Bearer ${adminToken}`, | ||
}; | ||
return impersonationOpts | ||
? withImpersonation(headers, impersonationOpts) | ||
: headers; | ||
} | ||
@@ -154,2 +178,3 @@ | ||
auth: Auth; | ||
impersonationOpts?: ImpersonationOpts; | ||
@@ -162,2 +187,17 @@ constructor(_config: Config) { | ||
/** | ||
* Sometimes you want to scope queries to a specific user. | ||
* | ||
* You can provide a user's auth token, email, or impersonate a guest. | ||
* | ||
* @see https://docs.instantdb.com/docs/backend#impersonating-users | ||
* @example | ||
* await db.asUser({email: "stopa@instantdb.com"}).query({ goals: {} }) | ||
*/ | ||
asUser = (opts: ImpersonationOpts): InstantAdmin<Schema> => { | ||
const newClient = new InstantAdmin<Schema>({ ...this.config }); | ||
newClient.impersonationOpts = opts; | ||
return newClient; | ||
}; | ||
/** | ||
* Use this to query your data! | ||
@@ -182,3 +222,3 @@ * | ||
method: "POST", | ||
headers: authorizedHeaders(this.config), | ||
headers: authorizedHeaders(this.config, this.impersonationOpts), | ||
body: JSON.stringify({ query: query }), | ||
@@ -216,3 +256,3 @@ }); | ||
method: "POST", | ||
headers: authorizedHeaders(this.config), | ||
headers: authorizedHeaders(this.config, this.impersonationOpts), | ||
body: JSON.stringify({ steps: steps }), | ||
@@ -219,0 +259,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
69273
1596
+ Added@instantdb/core@0.10.5(transitive)
- Removed@instantdb/core@0.10.4(transitive)
Updated@instantdb/core@0.10.5