Skip to content

Commit

Permalink
Merge pull request #23 from desci-labs/attestation-routing
Browse files Browse the repository at this point in the history
Attestation Routing
  • Loading branch information
hubsmoke authored Jun 21, 2024
2 parents 841b096 + 5e189bf commit b33716a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/dpid-reader/DpidReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,24 @@ export class DpidReader {
private static transformWeb = async (result: DpidResult, request: DpidRequest) => {
const { prefix, suffix, version, domain } = request;
const uuid = result.id64;
// debugger;
const output = { msg: `beta.dpid.org resolver`, params: request, uuid };

// TODO: support version=v1 syntax in Nodes and we can get rid of cleanVersion logic
const cleanVersion: string | undefined = !version
let cleanVersion: string | undefined = !version
? undefined
: version?.substring(0, 1) == "v"
? version
: `v${parseInt(version || "0") + 1}`;

if (cleanVersion === "vNaN") {
/**
** Not a valid version, so pass along the original string the user entered, it may route to other
** codex entities or the DPID may be an alias to another external non research object entity.
*/
cleanVersion = version;
}

let environment = prefix === "beta-dev" || domain === "dev-beta.dpid.org" ? "-dev" : "";
if (domain === "staging-beta.dpid.org") {
environment = "-staging";
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ app.get("/*", async (req: Request, res: Response) => {
extra: dpidRequest,
eventType: LogEventType.DPID_GET,
});

const dpidResult = await DpidReader.read(dpidRequest);
if (dpidResult.id16 == "0x0") {
logger.error("dpid not found");
throw new Error("dpid not found");
}
const redir = await DpidReader.transform(dpidResult, dpidRequest);
// debugger;
// res.send({ output, redir });
if (dpidRequest.jsonld) {
res.setHeader("Content-Type", "application/ld+json").send(redir);
Expand Down
96 changes: 96 additions & 0 deletions test/basics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,101 @@ describe("dPID resolution", function () {
assert.equal(value, expected, "incorrect resolution");
});
});
it("should handle a generic attestations route", async () => {
await request(app)
.get("/46/attestations")
.expect(302)
.then((res) => {
const value = res.header["location"];

const expected = "https://nodes.desci.com/dpid/46/attestations";
assert.equal(value, expected, "incorrect resolution");
})
.catch((err) => {
if (err) {
assert.fail(err);
}
});
});
it("should handle a versioned(V) generic attestations route", async () => {
await request(app)
.get("/46/v2/attestations")
.expect(302)
.then((res) => {
const value = res.header["location"];

const expected = "https://nodes.desci.com/dpid/46/v2/attestations";
assert.equal(value, expected, "incorrect resolution");
})
.catch((err) => {
if (err) {
assert.fail(err);
}
});
});
it("should handle a versioned(I) generic attestations route", async () => {
await request(app)
.get("/46/2/attestations")
.expect(302)
.then((res) => {
const value = res.header["location"];

const expected = "https://nodes.desci.com/dpid/46/v3/attestations";
assert.equal(value, expected, "incorrect resolution");
})
.catch((err) => {
if (err) {
assert.fail(err);
}
});
});
it("should handle a specific attestations route with an attestation slug", async () => {
await request(app)
.get("/46/attestations/scientific-manuscript")
.expect(302)
.then((res) => {
const value = res.header["location"];

const expected = "https://nodes.desci.com/dpid/46/attestations/scientific-manuscript";
assert.equal(value, expected, "incorrect resolution");
})
.catch((err) => {
if (err) {
assert.fail(err);
}
});
});
it("should handle a versioned(V) attestations route with an attestation slug", async () => {
await request(app)
.get("/46/v2/attestations/scientific-manuscript")
.expect(302)
.then((res) => {
const value = res.header["location"];

const expected = "https://nodes.desci.com/dpid/46/v2/attestations/scientific-manuscript";
assert.equal(value, expected, "incorrect resolution");
})
.catch((err) => {
if (err) {
assert.fail(err);
}
});
});
it("should handle a versioned(I) attestations route with an attestation slug", async () => {
await request(app)
.get("/46/2/attestations/scientific-manuscript")
.expect(302)
.then((res) => {
const value = res.header["location"];

const expected = "https://nodes.desci.com/dpid/46/v3/attestations/scientific-manuscript";
assert.equal(value, expected, "incorrect resolution");
})
.catch((err) => {
if (err) {
assert.fail(err);
}
});
});
});
});
8 changes: 4 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"strict": true,
"skipLibCheck": true,
"baseUrl": "./src",
"inlineSources": true,
"inlineSourceMap": true
},
"include": [
"src/**/*"
],
}
"include": ["src/**/*"]
}

0 comments on commit b33716a

Please sign in to comment.