Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ app.get('/users', {

await app.register(pman, {
postmanApiKey: 'PMAK-…',
// Either pass workspaceId directly...
workspaceId: '00000000-0000-4000-8000-000000000000',
// ...or pass a workspace link and let pman extract the id:
// workspaceLink: 'https://<team>.postman.co/workspace/My~00000000-0000-4000-8000-000000000000/overview',
postmanBaseUrl: 'http://127.0.0.1:3000',
collectionName: 'My API',
folderStrategy: 'path',
Expand Down Expand Up @@ -77,6 +80,7 @@ Secrets are never written to the sync state file.
| Option | Description |
|--------|-------------|
| `workspaceId` | Postman workspace id |
| `workspaceLink` | Postman workspace link (extracts `workspaceId` automatically) |
| `postmanApiKey` | Postman API key |
| `postmanBaseUrl` | Value for Postman variable `baseUrl` (`{{baseUrl}}` in URLs) |
| `reuseExistingCollectionByName` | Reuse workspace collection with same name when no state file (default `true`) |
Expand Down
2 changes: 1 addition & 1 deletion examples/playground.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ async function main() {
postmanApiKey: postman.postmanApiKey,
workspaceId: postman.workspaceId,
postmanBaseUrl: publicBase,
collectionName: 'Shiftr / pman playground',
collectionName: 'pman ~ by st3ix',
});

await fastify.listen({ port, host: '127.0.0.1' });
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@st3ix/pman",
"version": "1.0.1",
"version": "1.0.2",
"description": "Sync Fastify route schemas to Postman via OpenAPI and the Postman API.",
"type": "module",
"main": "./dist/index.js",
Expand Down
36 changes: 35 additions & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export type FolderStrategy = 'path' | 'tags' | 'hybrid';

export type FastifyPmanOptions = {
workspaceId?: string;
/** Optional Postman workspace URL. If set, the workspace id is extracted automatically. */
workspaceLink?: string;
collectionName?: string;
statePath?: string;
dryRun?: boolean;
Expand Down Expand Up @@ -58,6 +60,36 @@ function firstNonEmpty(...candidates: (string | undefined)[]): string | undefine
return undefined;
}

function wspacelink(link: string | undefined): string | undefined {
if (typeof link !== 'string') return undefined;
const raw = link.trim();
if (!raw) return undefined;

let u: URL;
try {
u = new URL(raw);
} catch {
return undefined;
}

const parts = u.pathname.split('/').filter(Boolean);
const i = parts.indexOf('workspace');
if (i < 0) return undefined;
const seg = parts[i + 1];
if (typeof seg !== 'string' || seg.length === 0) return undefined;

// Typical format: "<name>~<uuid>"
const afterTilde = seg.includes('~') ? seg.split('~').pop() : seg;
const id = (afterTilde ?? '').trim();
if (!id) return undefined;

// Very small sanity check: UUID-ish.
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(id)) {
return undefined;
}
return id;
}

export function resolvePmanOptions(opts: FastifyPmanOptions): ResolvedPmanOptions {
const auth =
opts.auth && typeof opts.auth === 'object'
Expand All @@ -73,8 +105,10 @@ export function resolvePmanOptions(opts: FastifyPmanOptions): ResolvedPmanOption
}
: null;

const extractedWorkspaceId = wspacelink(opts.workspaceLink);

return {
workspaceId: firstNonEmpty(opts.workspaceId, process.env.POSTMAN_WORKSPACE_ID),
workspaceId: firstNonEmpty(opts.workspaceId, extractedWorkspaceId, process.env.POSTMAN_WORKSPACE_ID),
collectionName: opts.collectionName?.trim() || 'Fastify (pman)',
statePath: opts.statePath ?? `${process.cwd()}/.postman-sync.json`,
dryRun: opts.dryRun ?? false,
Expand Down
Loading