@atproto/api 0.13.3 → 0.13.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/OAUTH.md +24 -13
- package/README.md +17 -7
- package/dist/agent.d.ts +17 -8
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +59 -6
- package/dist/agent.js.map +1 -1
- package/dist/atp-agent.d.ts +32 -18
- package/dist/atp-agent.d.ts.map +1 -1
- package/dist/atp-agent.js +117 -103
- package/dist/atp-agent.js.map +1 -1
- package/dist/client/lexicons.d.ts +4 -0
- package/dist/client/lexicons.d.ts.map +1 -1
- package/dist/client/lexicons.js +4 -0
- package/dist/client/lexicons.js.map +1 -1
- package/dist/client/types/app/bsky/feed/getPostThread.d.ts +1 -0
- package/dist/client/types/app/bsky/feed/getPostThread.d.ts.map +1 -1
- package/dist/client/types/app/bsky/feed/getPostThread.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -3
- package/dist/index.js.map +1 -1
- package/dist/session-manager.d.ts +5 -0
- package/dist/session-manager.d.ts.map +1 -0
- package/dist/session-manager.js +3 -0
- package/dist/session-manager.js.map +1 -0
- package/package.json +2 -2
- package/src/agent.ts +35 -14
- package/src/atp-agent.ts +149 -128
- package/src/client/lexicons.ts +4 -0
- package/src/client/types/app/bsky/feed/getPostThread.ts +1 -0
- package/src/index.ts +5 -2
- package/src/session-manager.ts +5 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getPostThread.d.ts","sourceRoot":"","sources":["../../../../../../src/client/types/app/bsky/feed/getPostThread.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAKrD,OAAO,KAAK,eAAe,MAAM,QAAQ,CAAA;AAEzC,MAAM,WAAW,WAAW;IAC1B,yCAAyC;IACzC,GAAG,EAAE,MAAM,CAAA;IACX,qEAAqE;IACrE,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,wEAAwE;IACxE,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB;AAED,MAAM,MAAM,WAAW,GAAG,SAAS,CAAA;AAEnC,MAAM,WAAW,YAAY;IAC3B,MAAM,EACF,eAAe,CAAC,cAAc,GAC9B,eAAe,CAAC,YAAY,GAC5B,eAAe,CAAC,WAAW,GAC3B;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,CAAA;IAC3C,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;CACrB;AAED,MAAM,WAAW,WAAW;IAC1B,MAAM,CAAC,EAAE,WAAW,CAAA;IACpB,OAAO,CAAC,EAAE,UAAU,CAAA;CACrB;AAED,MAAM,WAAW,QAAQ;IACvB,OAAO,EAAE,OAAO,CAAA;IAChB,OAAO,EAAE,UAAU,CAAA;IACnB,IAAI,EAAE,YAAY,CAAA;CACnB;AAED,qBAAa,aAAc,SAAQ,SAAS;gBAC9B,GAAG,EAAE,SAAS;CAG3B;AAED,wBAAgB,UAAU,CAAC,CAAC,EAAE,GAAG,OAMhC"}
|
|
1
|
+
{"version":3,"file":"getPostThread.d.ts","sourceRoot":"","sources":["../../../../../../src/client/types/app/bsky/feed/getPostThread.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAKrD,OAAO,KAAK,eAAe,MAAM,QAAQ,CAAA;AAEzC,MAAM,WAAW,WAAW;IAC1B,yCAAyC;IACzC,GAAG,EAAE,MAAM,CAAA;IACX,qEAAqE;IACrE,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,wEAAwE;IACxE,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB;AAED,MAAM,MAAM,WAAW,GAAG,SAAS,CAAA;AAEnC,MAAM,WAAW,YAAY;IAC3B,MAAM,EACF,eAAe,CAAC,cAAc,GAC9B,eAAe,CAAC,YAAY,GAC5B,eAAe,CAAC,WAAW,GAC3B;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,CAAA;IAC3C,UAAU,CAAC,EAAE,eAAe,CAAC,cAAc,CAAA;IAC3C,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;CACrB;AAED,MAAM,WAAW,WAAW;IAC1B,MAAM,CAAC,EAAE,WAAW,CAAA;IACpB,OAAO,CAAC,EAAE,UAAU,CAAA;CACrB;AAED,MAAM,WAAW,QAAQ;IACvB,OAAO,EAAE,OAAO,CAAA;IAChB,OAAO,EAAE,UAAU,CAAA;IACnB,IAAI,EAAE,YAAY,CAAA;CACnB;AAED,qBAAa,aAAc,SAAQ,SAAS;gBAC9B,GAAG,EAAE,SAAS;CAG3B;AAED,wBAAgB,UAAU,CAAC,CAAC,EAAE,GAAG,OAMhC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getPostThread.js","sourceRoot":"","sources":["../../../../../../src/client/types/app/bsky/feed/getPostThread.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,wCAAqD;
|
|
1
|
+
{"version":3,"file":"getPostThread.js","sourceRoot":"","sources":["../../../../../../src/client/types/app/bsky/feed/getPostThread.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,wCAAqD;AAuCrD,MAAa,aAAc,SAAQ,gBAAS;IAC1C,YAAY,GAAc;QACxB,KAAK,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAA;IACxE,CAAC;CACF;AAJD,sCAIC;AAED,SAAgB,UAAU,CAAC,CAAM;IAC/B,IAAI,CAAC,YAAY,gBAAS,EAAE,CAAC;QAC3B,IAAI,CAAC,CAAC,KAAK,KAAK,UAAU;YAAE,OAAO,IAAI,aAAa,CAAC,CAAC,CAAC,CAAA;IACzD,CAAC;IAED,OAAO,CAAC,CAAA;AACV,CAAC;AAND,gCAMC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -16,8 +16,10 @@ export * from './mocker';
|
|
|
16
16
|
export { LABELS, DEFAULT_LABEL_SETTINGS } from './moderation/const/labels';
|
|
17
17
|
export { Agent } from './agent';
|
|
18
18
|
export { AtpAgent, type AtpAgentOptions } from './atp-agent';
|
|
19
|
+
export { CredentialSession } from './atp-agent';
|
|
19
20
|
export { BskyAgent } from './bsky-agent';
|
|
21
|
+
export {
|
|
20
22
|
/** @deprecated */
|
|
21
|
-
|
|
23
|
+
AtpAgent as default, } from './atp-agent';
|
|
22
24
|
export declare const lexicons: Lexicons;
|
|
23
25
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AAG3C,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAA;AACvC,OAAO,EACL,OAAO,EACP,SAAS,EACT,YAAY,EACZ,SAAS,EACT,eAAe,GAChB,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AACnD,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA;AACvB,cAAc,QAAQ,CAAA;AACtB,cAAc,UAAU,CAAA;AACxB,cAAc,uBAAuB,CAAA;AACrC,cAAc,0BAA0B,CAAA;AACxC,cAAc,qBAAqB,CAAA;AACnC,cAAc,kBAAkB,CAAA;AAChC,cAAc,cAAc,CAAA;AAC5B,cAAc,oBAAoB,CAAA;AAClC,cAAc,UAAU,CAAA;AACxB,OAAO,EAAE,MAAM,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAA;AAC1E,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAE/B,OAAO,EAAE,QAAQ,EAAE,KAAK,eAAe,EAAE,MAAM,aAAa,CAAA;AAC5D,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAExC,kBAAkB;AAClB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AAG3C,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAA;AACvC,OAAO,EACL,OAAO,EACP,SAAS,EACT,YAAY,EACZ,SAAS,EACT,eAAe,GAChB,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AACnD,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA;AACvB,cAAc,QAAQ,CAAA;AACtB,cAAc,UAAU,CAAA;AACxB,cAAc,uBAAuB,CAAA;AACrC,cAAc,0BAA0B,CAAA;AACxC,cAAc,qBAAqB,CAAA;AACnC,cAAc,kBAAkB,CAAA;AAChC,cAAc,cAAc,CAAA;AAC5B,cAAc,oBAAoB,CAAA;AAClC,cAAc,UAAU,CAAA;AACxB,OAAO,EAAE,MAAM,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAA;AAC1E,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAE/B,OAAO,EAAE,QAAQ,EAAE,KAAK,eAAe,EAAE,MAAM,aAAa,CAAA;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAExC,OAAO;AACL,kBAAkB;AAClB,QAAQ,IAAI,OAAO,GACpB,MAAM,aAAa,CAAA;AAIpB,eAAO,MAAM,QAAQ,UAAiC,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.lexicons = exports.default = exports.BskyAgent = exports.AtpAgent = exports.Agent = exports.DEFAULT_LABEL_SETTINGS = exports.LABELS = exports.parseLanguage = exports.jsonStringToLex = exports.jsonToLex = exports.stringifyLex = exports.lexToJson = exports.BlobRef = exports.AtUri = void 0;
|
|
17
|
+
exports.lexicons = exports.default = exports.BskyAgent = exports.CredentialSession = exports.AtpAgent = exports.Agent = exports.DEFAULT_LABEL_SETTINGS = exports.LABELS = exports.parseLanguage = exports.jsonStringToLex = exports.jsonToLex = exports.stringifyLex = exports.lexToJson = exports.BlobRef = exports.AtUri = void 0;
|
|
18
18
|
const lexicon_1 = require("@atproto/lexicon");
|
|
19
19
|
const lexicons_1 = require("./client/lexicons");
|
|
20
20
|
var syntax_1 = require("@atproto/syntax");
|
|
@@ -45,11 +45,13 @@ var agent_1 = require("./agent");
|
|
|
45
45
|
Object.defineProperty(exports, "Agent", { enumerable: true, get: function () { return agent_1.Agent; } });
|
|
46
46
|
var atp_agent_1 = require("./atp-agent");
|
|
47
47
|
Object.defineProperty(exports, "AtpAgent", { enumerable: true, get: function () { return atp_agent_1.AtpAgent; } });
|
|
48
|
+
var atp_agent_2 = require("./atp-agent");
|
|
49
|
+
Object.defineProperty(exports, "CredentialSession", { enumerable: true, get: function () { return atp_agent_2.CredentialSession; } });
|
|
48
50
|
var bsky_agent_1 = require("./bsky-agent");
|
|
49
51
|
Object.defineProperty(exports, "BskyAgent", { enumerable: true, get: function () { return bsky_agent_1.BskyAgent; } });
|
|
52
|
+
var atp_agent_3 = require("./atp-agent");
|
|
50
53
|
/** @deprecated */
|
|
51
|
-
|
|
52
|
-
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return atp_agent_2.AtpAgent; } });
|
|
54
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return atp_agent_3.AtpAgent; } });
|
|
53
55
|
// Expose a copy to prevent alteration of the internal Lexicon instance used by
|
|
54
56
|
// the AtpBaseClient class.
|
|
55
57
|
exports.lexicons = new lexicon_1.Lexicons(lexicons_1.lexicons);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,8CAA2C;AAC3C,gDAAgE;AAEhE,0CAAuC;AAA9B,+FAAA,KAAK,OAAA;AACd,4CAMyB;AALvB,kGAAA,OAAO,OAAA;AACP,oGAAA,SAAS,OAAA;AACT,uGAAA,YAAY,OAAA;AACZ,oGAAA,SAAS,OAAA;AACT,0GAAA,eAAe,OAAA;AAEjB,kDAAmD;AAA1C,2GAAA,aAAa,OAAA;AACtB,0CAAuB;AACvB,0CAAuB;AACvB,yCAAsB;AACtB,2CAAwB;AACxB,wDAAqC;AACrC,2DAAwC;AACxC,sDAAmC;AACnC,mDAAgC;AAChC,+CAA4B;AAC5B,qDAAkC;AAClC,2CAAwB;AACxB,oDAA0E;AAAjE,gGAAA,MAAM,OAAA;AAAE,gHAAA,sBAAsB,OAAA;AACvC,iCAA+B;AAAtB,8FAAA,KAAK,OAAA;AAEd,yCAA4D;AAAnD,qGAAA,QAAQ,OAAA;AACjB,2CAAwC;AAA/B,uGAAA,SAAS,OAAA;AAElB,kBAAkB;AAClB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,8CAA2C;AAC3C,gDAAgE;AAEhE,0CAAuC;AAA9B,+FAAA,KAAK,OAAA;AACd,4CAMyB;AALvB,kGAAA,OAAO,OAAA;AACP,oGAAA,SAAS,OAAA;AACT,uGAAA,YAAY,OAAA;AACZ,oGAAA,SAAS,OAAA;AACT,0GAAA,eAAe,OAAA;AAEjB,kDAAmD;AAA1C,2GAAA,aAAa,OAAA;AACtB,0CAAuB;AACvB,0CAAuB;AACvB,yCAAsB;AACtB,2CAAwB;AACxB,wDAAqC;AACrC,2DAAwC;AACxC,sDAAmC;AACnC,mDAAgC;AAChC,+CAA4B;AAC5B,qDAAkC;AAClC,2CAAwB;AACxB,oDAA0E;AAAjE,gGAAA,MAAM,OAAA;AAAE,gHAAA,sBAAsB,OAAA;AACvC,iCAA+B;AAAtB,8FAAA,KAAK,OAAA;AAEd,yCAA4D;AAAnD,qGAAA,QAAQ,OAAA;AACjB,yCAA+C;AAAtC,8GAAA,iBAAiB,OAAA;AAC1B,2CAAwC;AAA/B,uGAAA,SAAS,OAAA;AAElB,yCAGoB;AAFlB,kBAAkB;AAClB,oGAAA,QAAQ,OAAW;AAGrB,+EAA+E;AAC/E,2BAA2B;AACd,QAAA,QAAQ,GAAG,IAAI,kBAAQ,CAAC,mBAAgB,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-manager.d.ts","sourceRoot":"","sources":["../src/session-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AAElD,MAAM,WAAW,cAAe,SAAQ,kBAAkB;IACxD,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CACtB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-manager.js","sourceRoot":"","sources":["../src/session-manager.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atproto/api",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.4",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Client library for atproto and Bluesky",
|
|
6
6
|
"keywords": [
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"@atproto/common-web": "^0.3.0",
|
|
24
24
|
"@atproto/lexicon": "^0.4.1",
|
|
25
25
|
"@atproto/syntax": "^0.3.0",
|
|
26
|
-
"@atproto/xrpc": "^0.6.
|
|
26
|
+
"@atproto/xrpc": "^0.6.1"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"get-port": "^6.1.2",
|
package/src/agent.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { TID } from '@atproto/common-web'
|
|
2
2
|
import { AtUri, ensureValidDid } from '@atproto/syntax'
|
|
3
|
-
import {
|
|
4
|
-
buildFetchHandler,
|
|
5
|
-
FetchHandler,
|
|
6
|
-
FetchHandlerOptions,
|
|
7
|
-
} from '@atproto/xrpc'
|
|
3
|
+
import { buildFetchHandler, FetchHandler, XrpcClient } from '@atproto/xrpc'
|
|
8
4
|
import AwaitLock from 'await-lock'
|
|
9
5
|
import {
|
|
10
6
|
AppBskyActorDefs,
|
|
11
7
|
AppBskyActorProfile,
|
|
12
8
|
AppBskyFeedPost,
|
|
13
9
|
AppBskyLabelerDefs,
|
|
14
|
-
|
|
10
|
+
AppNS,
|
|
11
|
+
ChatNS,
|
|
15
12
|
ComAtprotoRepoPutRecord,
|
|
13
|
+
ComNS,
|
|
14
|
+
ToolsNS,
|
|
16
15
|
} from './client/index'
|
|
16
|
+
import { schemas } from './client/lexicons'
|
|
17
17
|
import { MutedWord } from './client/types/app/bsky/actor/defs'
|
|
18
18
|
import { BSKY_LABELER_DID } from './const'
|
|
19
19
|
import { interpretLabelValueDefinitions } from './moderation'
|
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
LabelPreference,
|
|
24
24
|
ModerationPrefs,
|
|
25
25
|
} from './moderation/types'
|
|
26
|
+
import { SessionManager } from './session-manager'
|
|
26
27
|
import {
|
|
27
28
|
AtpAgentGlobalOpts,
|
|
28
29
|
AtprotoServiceType,
|
|
@@ -68,14 +69,13 @@ export type { FetchHandler }
|
|
|
68
69
|
/**
|
|
69
70
|
* An {@link Agent} is an {@link AtpBaseClient} with the following
|
|
70
71
|
* additional features:
|
|
71
|
-
* - Abstract session management utilities
|
|
72
72
|
* - AT Protocol labelers configuration utilities
|
|
73
73
|
* - AT Protocol proxy configuration utilities
|
|
74
74
|
* - Cloning utilities
|
|
75
75
|
* - `app.bsky` syntactic sugar
|
|
76
76
|
* - `com.atproto` syntactic sugar
|
|
77
77
|
*/
|
|
78
|
-
export
|
|
78
|
+
export class Agent extends XrpcClient {
|
|
79
79
|
//#region Static configuration
|
|
80
80
|
|
|
81
81
|
/**
|
|
@@ -94,8 +94,18 @@ export abstract class Agent extends AtpBaseClient {
|
|
|
94
94
|
|
|
95
95
|
//#endregion
|
|
96
96
|
|
|
97
|
-
|
|
98
|
-
|
|
97
|
+
com = new ComNS(this)
|
|
98
|
+
app = new AppNS(this)
|
|
99
|
+
chat = new ChatNS(this)
|
|
100
|
+
tools = new ToolsNS(this)
|
|
101
|
+
|
|
102
|
+
/** @deprecated use `this` instead */
|
|
103
|
+
get xrpc(): XrpcClient {
|
|
104
|
+
return this
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
constructor(readonly sessionManager: SessionManager) {
|
|
108
|
+
const fetchHandler = buildFetchHandler(sessionManager)
|
|
99
109
|
|
|
100
110
|
super((url, init) => {
|
|
101
111
|
const headers = new Headers(init?.headers)
|
|
@@ -118,16 +128,20 @@ export abstract class Agent extends AtpBaseClient {
|
|
|
118
128
|
)
|
|
119
129
|
|
|
120
130
|
return fetchHandler(url, { ...init, headers })
|
|
121
|
-
})
|
|
131
|
+
}, schemas)
|
|
122
132
|
}
|
|
123
133
|
|
|
124
134
|
//#region Cloning utilities
|
|
125
135
|
|
|
126
|
-
|
|
136
|
+
clone(): Agent {
|
|
137
|
+
return this.copyInto(new Agent(this.sessionManager))
|
|
138
|
+
}
|
|
127
139
|
|
|
128
140
|
copyInto<T extends Agent>(inst: T): T {
|
|
129
141
|
inst.configureLabelers(this.labelers)
|
|
130
142
|
inst.configureProxy(this.proxy ?? null)
|
|
143
|
+
inst.clearHeaders()
|
|
144
|
+
for (const [key, value] of this.headers) inst.setHeader(key, value)
|
|
131
145
|
return inst
|
|
132
146
|
}
|
|
133
147
|
|
|
@@ -185,12 +199,19 @@ export abstract class Agent extends AtpBaseClient {
|
|
|
185
199
|
/**
|
|
186
200
|
* Get the authenticated user's DID, if any.
|
|
187
201
|
*/
|
|
188
|
-
|
|
202
|
+
get did() {
|
|
203
|
+
return this.sessionManager.did
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** @deprecated Use {@link Agent.assertDid} instead */
|
|
207
|
+
get accountDid() {
|
|
208
|
+
return this.assertDid
|
|
209
|
+
}
|
|
189
210
|
|
|
190
211
|
/**
|
|
191
212
|
* Get the authenticated user's DID, or throw an error if not authenticated.
|
|
192
213
|
*/
|
|
193
|
-
|
|
214
|
+
get assertDid(): string {
|
|
194
215
|
this.assertAuthenticated()
|
|
195
216
|
return this.did
|
|
196
217
|
}
|
package/src/atp-agent.ts
CHANGED
|
@@ -4,16 +4,18 @@ import {
|
|
|
4
4
|
Gettable,
|
|
5
5
|
ResponseType,
|
|
6
6
|
XRPCError,
|
|
7
|
-
|
|
7
|
+
XrpcClient,
|
|
8
8
|
errorResponseBody,
|
|
9
9
|
} from '@atproto/xrpc'
|
|
10
10
|
import { Agent } from './agent'
|
|
11
11
|
import {
|
|
12
|
-
AtpBaseClient,
|
|
13
12
|
ComAtprotoServerCreateAccount,
|
|
14
13
|
ComAtprotoServerCreateSession,
|
|
15
14
|
ComAtprotoServerGetSession,
|
|
15
|
+
ComAtprotoServerNS,
|
|
16
16
|
} from './client'
|
|
17
|
+
import { schemas } from './client/lexicons'
|
|
18
|
+
import { SessionManager } from './session-manager'
|
|
17
19
|
import {
|
|
18
20
|
AtpAgentLoginOpts,
|
|
19
21
|
AtpPersistSessionHandler,
|
|
@@ -32,92 +34,44 @@ export type AtpAgentOptions = {
|
|
|
32
34
|
}
|
|
33
35
|
|
|
34
36
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
+
* A wrapper around the {@link Agent} class that uses credential based session
|
|
38
|
+
* management. This class also exposes most of the session management methods
|
|
39
|
+
* directly.
|
|
40
|
+
*
|
|
41
|
+
* This class will be deprecated in the near future. Use {@link Agent} directly
|
|
42
|
+
* with a {@link CredentialSession} instead:
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* const session = new CredentialSession({
|
|
46
|
+
* service: new URL('https://example.com'),
|
|
47
|
+
* })
|
|
48
|
+
*
|
|
49
|
+
* const agent = new Agent(session)
|
|
50
|
+
* ```
|
|
37
51
|
*/
|
|
38
52
|
export class AtpAgent extends Agent {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if (!this.session?.refreshJwt) {
|
|
62
|
-
return initialRes
|
|
63
|
-
}
|
|
64
|
-
const isExpiredToken = await isErrorResponse(
|
|
65
|
-
initialRes,
|
|
66
|
-
[400],
|
|
67
|
-
['ExpiredToken'],
|
|
68
|
-
)
|
|
69
|
-
|
|
70
|
-
if (!isExpiredToken) {
|
|
71
|
-
return initialRes
|
|
53
|
+
readonly sessionManager: CredentialSession
|
|
54
|
+
|
|
55
|
+
constructor(options: AtpAgentOptions | CredentialSession) {
|
|
56
|
+
const sessionManager =
|
|
57
|
+
options instanceof CredentialSession
|
|
58
|
+
? options
|
|
59
|
+
: new CredentialSession(
|
|
60
|
+
new URL(options.service),
|
|
61
|
+
options.fetch,
|
|
62
|
+
options.persistSession,
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
super(sessionManager)
|
|
66
|
+
|
|
67
|
+
// This assignment is already being done in the super constructor, but we
|
|
68
|
+
// need to do it here to make TypeScript happy.
|
|
69
|
+
this.sessionManager = sessionManager
|
|
70
|
+
|
|
71
|
+
if (!(options instanceof CredentialSession) && options.headers) {
|
|
72
|
+
for (const [key, value] of options.headers) {
|
|
73
|
+
this.setHeader(key, value)
|
|
72
74
|
}
|
|
73
|
-
|
|
74
|
-
try {
|
|
75
|
-
await this.sessionManager.refreshSession()
|
|
76
|
-
} catch {
|
|
77
|
-
return initialRes
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (reqInit?.signal?.aborted) {
|
|
81
|
-
return initialRes
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// The stream was already consumed. We cannot retry the request. A solution
|
|
85
|
-
// would be to tee() the input stream but that would bufferize the entire
|
|
86
|
-
// stream in memory which can lead to memory starvation. Instead, we will
|
|
87
|
-
// return the original response and let the calling code handle retries.
|
|
88
|
-
if (ReadableStream && reqInit.body instanceof ReadableStream) {
|
|
89
|
-
return initialRes
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// Return initial "ExpiredToken" response if the session was not refreshed.
|
|
93
|
-
const updatedToken = this.session?.accessJwt
|
|
94
|
-
if (!updatedToken || updatedToken === initialToken) {
|
|
95
|
-
return initialRes
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Make sure the initial request is cancelled to avoid leaking resources
|
|
99
|
-
// (NodeJS 👀): https://undici.nodejs.org/#/?id=garbage-collection
|
|
100
|
-
await initialRes.body?.cancel()
|
|
101
|
-
|
|
102
|
-
// We need to re-compute the URI in case the PDS endpoint has changed
|
|
103
|
-
const updatedUri = new URL(url, this.dispatchUrl)
|
|
104
|
-
const updatedReq = new Request(updatedUri, reqInit)
|
|
105
|
-
|
|
106
|
-
updatedReq.headers.set('authorization', `Bearer ${updatedToken}`)
|
|
107
|
-
|
|
108
|
-
return await (0, this.sessionManager.fetch)(updatedReq)
|
|
109
|
-
})
|
|
110
|
-
|
|
111
|
-
if (options instanceof SessionManager) {
|
|
112
|
-
this.headers = new Map()
|
|
113
|
-
this.sessionManager = options
|
|
114
|
-
} else {
|
|
115
|
-
this.headers = new Map(options.headers)
|
|
116
|
-
this.sessionManager = new SessionManager(
|
|
117
|
-
new URL(options.service),
|
|
118
|
-
options.fetch,
|
|
119
|
-
options.persistSession,
|
|
120
|
-
)
|
|
121
75
|
}
|
|
122
76
|
}
|
|
123
77
|
|
|
@@ -125,36 +79,16 @@ export class AtpAgent extends Agent {
|
|
|
125
79
|
return this.copyInto(new AtpAgent(this.sessionManager))
|
|
126
80
|
}
|
|
127
81
|
|
|
128
|
-
copyInto<T extends Agent>(inst: T): T {
|
|
129
|
-
if (inst instanceof AtpAgent) {
|
|
130
|
-
for (const [key] of inst.headers) {
|
|
131
|
-
inst.unsetHeader(key)
|
|
132
|
-
}
|
|
133
|
-
for (const [key, value] of this.headers) {
|
|
134
|
-
inst.setHeader(key, value)
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
return super.copyInto(inst)
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
setHeader(key: string, value: Gettable<null | string>): void {
|
|
141
|
-
this.headers.set(key.toLowerCase(), value)
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
unsetHeader(key: string): void {
|
|
145
|
-
this.headers.delete(key.toLowerCase())
|
|
146
|
-
}
|
|
147
|
-
|
|
148
82
|
get session() {
|
|
149
83
|
return this.sessionManager.session
|
|
150
84
|
}
|
|
151
85
|
|
|
152
86
|
get hasSession() {
|
|
153
|
-
return
|
|
87
|
+
return this.sessionManager.hasSession
|
|
154
88
|
}
|
|
155
89
|
|
|
156
90
|
get did() {
|
|
157
|
-
return this.
|
|
91
|
+
return this.sessionManager.did
|
|
158
92
|
}
|
|
159
93
|
|
|
160
94
|
get serviceUrl() {
|
|
@@ -166,7 +100,7 @@ export class AtpAgent extends Agent {
|
|
|
166
100
|
}
|
|
167
101
|
|
|
168
102
|
get dispatchUrl() {
|
|
169
|
-
return this.
|
|
103
|
+
return this.sessionManager.dispatchUrl
|
|
170
104
|
}
|
|
171
105
|
|
|
172
106
|
/** @deprecated use {@link serviceUrl} instead */
|
|
@@ -186,7 +120,7 @@ export class AtpAgent extends Agent {
|
|
|
186
120
|
)
|
|
187
121
|
}
|
|
188
122
|
|
|
189
|
-
/** @deprecated
|
|
123
|
+
/** @deprecated use {@link AtpAgent.serviceUrl} instead */
|
|
190
124
|
getServiceUrl() {
|
|
191
125
|
return this.serviceUrl
|
|
192
126
|
}
|
|
@@ -216,23 +150,34 @@ export class AtpAgent extends Agent {
|
|
|
216
150
|
}
|
|
217
151
|
|
|
218
152
|
/**
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
*
|
|
153
|
+
* Credentials (username / password) based session manager. Instances of this
|
|
154
|
+
* class will typically be used as the session manager for an {@link AtpAgent}.
|
|
155
|
+
* They can also be used with an {@link XrpcClient}, if you want to use you
|
|
156
|
+
* own Lexicons.
|
|
222
157
|
*/
|
|
223
|
-
class SessionManager {
|
|
158
|
+
export class CredentialSession implements SessionManager {
|
|
224
159
|
public pdsUrl?: URL // The PDS URL, driven by the did doc
|
|
225
160
|
public session?: AtpSessionData
|
|
226
161
|
public refreshSessionPromise: Promise<void> | undefined
|
|
227
162
|
|
|
228
163
|
/**
|
|
229
|
-
* Private {@link
|
|
164
|
+
* Private {@link ComAtprotoServerNS} used to perform session management API
|
|
230
165
|
* calls on the service endpoint. Calls performed by this agent will not be
|
|
231
|
-
* authenticated using the user's session
|
|
166
|
+
* authenticated using the user's session to allow proper manual configuration
|
|
167
|
+
* of the headers when performing session management operations.
|
|
232
168
|
*/
|
|
233
|
-
protected
|
|
234
|
-
|
|
235
|
-
|
|
169
|
+
protected server = new ComAtprotoServerNS(
|
|
170
|
+
// Note that the use of the codegen "schemas" (to instantiate `this.api`),
|
|
171
|
+
// as well as the use of `ComAtprotoServerNS` will cause this class to
|
|
172
|
+
// reference (way) more code than it actually needs. It is not possible,
|
|
173
|
+
// with the current state of the codegen, to generate a client that only
|
|
174
|
+
// includes the methods that are actually used by this class. This is a
|
|
175
|
+
// known limitation that should be addressed in a future version of the
|
|
176
|
+
// codegen.
|
|
177
|
+
new XrpcClient((url, init) => {
|
|
178
|
+
return (0, this.fetch)(new URL(url, this.serviceUrl), init)
|
|
179
|
+
}, schemas),
|
|
180
|
+
)
|
|
236
181
|
|
|
237
182
|
constructor(
|
|
238
183
|
public readonly serviceUrl: URL,
|
|
@@ -240,6 +185,18 @@ class SessionManager {
|
|
|
240
185
|
protected readonly persistSession?: AtpPersistSessionHandler,
|
|
241
186
|
) {}
|
|
242
187
|
|
|
188
|
+
get did() {
|
|
189
|
+
return this.session?.did
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
get dispatchUrl() {
|
|
193
|
+
return this.pdsUrl || this.serviceUrl
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
get hasSession() {
|
|
197
|
+
return !!this.session
|
|
198
|
+
}
|
|
199
|
+
|
|
243
200
|
/**
|
|
244
201
|
* Sets a WhatWG "fetch()" function to be used for making HTTP requests.
|
|
245
202
|
*/
|
|
@@ -247,6 +204,71 @@ class SessionManager {
|
|
|
247
204
|
this.fetch = fetch
|
|
248
205
|
}
|
|
249
206
|
|
|
207
|
+
async fetchHandler(url: string, init?: RequestInit): Promise<Response> {
|
|
208
|
+
// wait for any active session-refreshes to finish
|
|
209
|
+
await this.refreshSessionPromise
|
|
210
|
+
|
|
211
|
+
const initialUri = new URL(url, this.dispatchUrl)
|
|
212
|
+
const initialReq = new Request(initialUri, init)
|
|
213
|
+
|
|
214
|
+
const initialToken = this.session?.accessJwt
|
|
215
|
+
if (!initialToken || initialReq.headers.has('authorization')) {
|
|
216
|
+
return (0, this.fetch)(initialReq)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
initialReq.headers.set('authorization', `Bearer ${initialToken}`)
|
|
220
|
+
const initialRes = await (0, this.fetch)(initialReq)
|
|
221
|
+
|
|
222
|
+
if (!this.session?.refreshJwt) {
|
|
223
|
+
return initialRes
|
|
224
|
+
}
|
|
225
|
+
const isExpiredToken = await isErrorResponse(
|
|
226
|
+
initialRes,
|
|
227
|
+
[400],
|
|
228
|
+
['ExpiredToken'],
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
if (!isExpiredToken) {
|
|
232
|
+
return initialRes
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
try {
|
|
236
|
+
await this.refreshSession()
|
|
237
|
+
} catch {
|
|
238
|
+
return initialRes
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (init?.signal?.aborted) {
|
|
242
|
+
return initialRes
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// The stream was already consumed. We cannot retry the request. A solution
|
|
246
|
+
// would be to tee() the input stream but that would bufferize the entire
|
|
247
|
+
// stream in memory which can lead to memory starvation. Instead, we will
|
|
248
|
+
// return the original response and let the calling code handle retries.
|
|
249
|
+
if (ReadableStream && init?.body instanceof ReadableStream) {
|
|
250
|
+
return initialRes
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Return initial "ExpiredToken" response if the session was not refreshed.
|
|
254
|
+
const updatedToken = this.session?.accessJwt
|
|
255
|
+
if (!updatedToken || updatedToken === initialToken) {
|
|
256
|
+
return initialRes
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// Make sure the initial request is cancelled to avoid leaking resources
|
|
260
|
+
// (NodeJS 👀): https://undici.nodejs.org/#/?id=garbage-collection
|
|
261
|
+
await initialRes.body?.cancel()
|
|
262
|
+
|
|
263
|
+
// We need to re-compute the URI in case the PDS endpoint has changed
|
|
264
|
+
const updatedUri = new URL(url, this.dispatchUrl)
|
|
265
|
+
const updatedReq = new Request(updatedUri, init)
|
|
266
|
+
|
|
267
|
+
updatedReq.headers.set('authorization', `Bearer ${updatedToken}`)
|
|
268
|
+
|
|
269
|
+
return await (0, this.fetch)(updatedReq)
|
|
270
|
+
}
|
|
271
|
+
|
|
250
272
|
/**
|
|
251
273
|
* Create a new account and hydrate its session in this agent.
|
|
252
274
|
*/
|
|
@@ -255,7 +277,7 @@ class SessionManager {
|
|
|
255
277
|
opts?: ComAtprotoServerCreateAccount.CallOptions,
|
|
256
278
|
): Promise<ComAtprotoServerCreateAccount.Response> {
|
|
257
279
|
try {
|
|
258
|
-
const res = await this.
|
|
280
|
+
const res = await this.server.createAccount(data, opts)
|
|
259
281
|
this.session = {
|
|
260
282
|
accessJwt: res.data.accessJwt,
|
|
261
283
|
refreshJwt: res.data.refreshJwt,
|
|
@@ -283,7 +305,7 @@ class SessionManager {
|
|
|
283
305
|
opts: AtpAgentLoginOpts,
|
|
284
306
|
): Promise<ComAtprotoServerCreateSession.Response> {
|
|
285
307
|
try {
|
|
286
|
-
const res = await this.
|
|
308
|
+
const res = await this.server.createSession({
|
|
287
309
|
identifier: opts.identifier,
|
|
288
310
|
password: opts.password,
|
|
289
311
|
authFactorToken: opts.authFactorToken,
|
|
@@ -312,7 +334,7 @@ class SessionManager {
|
|
|
312
334
|
async logout(): Promise<void> {
|
|
313
335
|
if (this.session) {
|
|
314
336
|
try {
|
|
315
|
-
await this.
|
|
337
|
+
await this.server.deleteSession(undefined, {
|
|
316
338
|
headers: {
|
|
317
339
|
authorization: `Bearer ${this.session.accessJwt}`,
|
|
318
340
|
},
|
|
@@ -335,7 +357,7 @@ class SessionManager {
|
|
|
335
357
|
this.session = session
|
|
336
358
|
|
|
337
359
|
try {
|
|
338
|
-
const res = await this.
|
|
360
|
+
const res = await this.server
|
|
339
361
|
.getSession(undefined, {
|
|
340
362
|
headers: { authorization: `Bearer ${session.accessJwt}` },
|
|
341
363
|
})
|
|
@@ -346,15 +368,14 @@ class SessionManager {
|
|
|
346
368
|
session.refreshJwt
|
|
347
369
|
) {
|
|
348
370
|
try {
|
|
349
|
-
const res = await this.
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
)
|
|
371
|
+
const res = await this.server.refreshSession(undefined, {
|
|
372
|
+
headers: { authorization: `Bearer ${session.refreshJwt}` },
|
|
373
|
+
})
|
|
353
374
|
|
|
354
375
|
session.accessJwt = res.data.accessJwt
|
|
355
376
|
session.refreshJwt = res.data.refreshJwt
|
|
356
377
|
|
|
357
|
-
return this.
|
|
378
|
+
return this.server.getSession(undefined, {
|
|
358
379
|
headers: { authorization: `Bearer ${session.accessJwt}` },
|
|
359
380
|
})
|
|
360
381
|
} catch {
|
|
@@ -425,7 +446,7 @@ class SessionManager {
|
|
|
425
446
|
}
|
|
426
447
|
|
|
427
448
|
try {
|
|
428
|
-
const res = await this.
|
|
449
|
+
const res = await this.server.refreshSession(undefined, {
|
|
429
450
|
headers: { authorization: `Bearer ${this.session.refreshJwt}` },
|
|
430
451
|
})
|
|
431
452
|
// succeeded, update the session
|
package/src/client/lexicons.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -25,10 +25,13 @@ export { LABELS, DEFAULT_LABEL_SETTINGS } from './moderation/const/labels'
|
|
|
25
25
|
export { Agent } from './agent'
|
|
26
26
|
|
|
27
27
|
export { AtpAgent, type AtpAgentOptions } from './atp-agent'
|
|
28
|
+
export { CredentialSession } from './atp-agent'
|
|
28
29
|
export { BskyAgent } from './bsky-agent'
|
|
29
30
|
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
export {
|
|
32
|
+
/** @deprecated */
|
|
33
|
+
AtpAgent as default,
|
|
34
|
+
} from './atp-agent'
|
|
32
35
|
|
|
33
36
|
// Expose a copy to prevent alteration of the internal Lexicon instance used by
|
|
34
37
|
// the AtpBaseClient class.
|