|
1 | | -const credentials = require('./credentials.json'); |
2 | 1 | const fs = require('fs/promises'); |
3 | 2 | const path = require('path'); |
4 | 3 |
|
5 | | -const API_URL = 'https://app.openops.com/api/v1/cloud-templates?version=9999'; |
| 4 | +const AUTH_URL = 'https://app.openops.com/api/v1/authentication/sign-in'; |
| 5 | +const TEMPLATES_URL = 'https://app.openops.com/api/v1/flow-templates?version=9999'; |
6 | 6 | const OUT_RELATIVE = '../snippets/templates-generated.mdx'; |
7 | 7 |
|
8 | 8 | const caseInsensitive = () => (a, b) => a.localeCompare(b, undefined, {sensitivity: 'base'}); |
@@ -53,27 +53,56 @@ const writeMarkdownToFile = async (lines, stats) => { |
53 | 53 | console.log(`Wrote ${stats.categories} categories, ${stats.names} template names to ${outPath}`); |
54 | 54 | }; |
55 | 55 |
|
56 | | -const main = async () => { |
| 56 | +const getAuthToken = async () => { |
| 57 | + let credentialsFile = {}; |
57 | 58 | try { |
58 | | - const res = await fetch(API_URL, { |
59 | | - headers: { |
60 | | - Accept: 'application/json', |
61 | | - Cookie: credentials.cookie |
62 | | - } |
| 59 | + credentialsFile = require('./credentials.json'); |
| 60 | + } catch { |
| 61 | + console.log('No credentials.json found. Using environment variables instead.'); |
| 62 | + } |
| 63 | + |
| 64 | + const username = process.env.OPENOPS_USERNAME || credentialsFile.username; |
| 65 | + const password = process.env.OPENOPS_PASSWORD || credentialsFile.password; |
| 66 | + |
| 67 | + const auth = await fetch(AUTH_URL, { |
| 68 | + method: "POST", |
| 69 | + headers: { |
| 70 | + "Content-Type": "application/json" |
| 71 | + }, |
| 72 | + body: JSON.stringify({ |
| 73 | + "email": username, |
| 74 | + "password": password |
| 75 | + } |
| 76 | + ) |
63 | 77 | }); |
64 | 78 |
|
65 | | - if (!res.ok) throw new Error(`Fetch failed: ${res.status}`); |
| 79 | + if (!auth.ok) throw new Error(`Unable to authenticate with app.openops.com: error ${auth.status}, ${auth.statusText}`); |
66 | 80 |
|
67 | | - const templates = await res.json(); |
68 | | - if (!Array.isArray(templates)) throw new Error('Unexpected API response: expected array'); |
| 81 | + return (await auth.json()).token; |
| 82 | +}; |
69 | 83 |
|
70 | | - const categoryMap = groupTemplatesByCategory(templates); |
71 | | - const {lines, stats} = prepareMarkdownLines(categoryMap, caseInsensitive); |
72 | | - await writeMarkdownToFile(lines, stats); |
73 | | - } catch (err) { |
74 | | - console.error('Error:', err?.message ?? err); |
75 | | - process.exitCode = 1; |
76 | | - } |
| 84 | +const main = async () => { |
| 85 | + try { |
| 86 | + const token = await getAuthToken() |
| 87 | + const res = await fetch(TEMPLATES_URL, { |
| 88 | + headers: { |
| 89 | + Accept: 'application/json', |
| 90 | + Authorization: `Bearer ${token}` |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + if (!res.ok) throw new Error(`Fetch failed: ${res.status}`); |
| 95 | + |
| 96 | + const templates = await res.json(); |
| 97 | + if (!Array.isArray(templates)) throw new Error('Unexpected API response: expected array'); |
| 98 | + |
| 99 | + const categoryMap = groupTemplatesByCategory(templates); |
| 100 | + const {lines, stats} = prepareMarkdownLines(categoryMap, caseInsensitive); |
| 101 | + await writeMarkdownToFile(lines, stats); |
| 102 | + } catch (err) { |
| 103 | + console.error('Error:', err?.message ?? err); |
| 104 | + process.exitCode = 1; |
| 105 | + } |
77 | 106 | }; |
78 | 107 |
|
79 | 108 | main(); |
0 commit comments