Skip to content

Commit

Permalink
Merge pull request #5 from rajnishdargan/main
Browse files Browse the repository at this point in the history
Integrating Middleware API for Editor
  • Loading branch information
itsvick authored Sep 28, 2024
2 parents 13a0d7f + 4b0a02c commit 25c33ec
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 85 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BASE_URL="" # Middleware Path - eg : "https://middleware.prathamdigital.org"
AUTH_API_TOKEN="" # User Auth Access Token
TENANT_ID="" # Tenant Id of User
18 changes: 5 additions & 13 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@ const nextConfig = {
async rewrites() {
return [
{
source: '/cats',
destination: 'https://meowfacts.herokuapp.com',
source: '/action/:path*', // Match any route starting with /action/
destination: '/api/proxy?path=/action/:path*', // Forward to the proxy API
},
{
source: '/ducks',
destination: 'https://random-d.uk/api/random',
},
{
source: '/action/:path*',
destination: 'http://localhost:4000/action/:path*', // Proxy to Backend
},
{
source: '/api/:path*',
destination: 'http://localhost:4000/api/:path*', // Proxy to Backend
},
source: '/api/:path*', // Match any route starting with /api/
destination: '/api/proxy?path=/api/:path*', // Forward to the proxy API
}
];
},
// Custom Webpack config
Expand Down
83 changes: 11 additions & 72 deletions src/components/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ export const questionSetEditorConfig = {
programId: '',
contributionOrgId: '',
user: {
id: '5a587cc1-e018-4859-a0a8-e842650b9d64',
fullName: 'Test User',
firstName: 'Test',
lastName: 'User',
orgIds: ['01269878797503692810']
id: 'ef99949b-7f3a-4a5f-806a-e67e683e38f3',
fullName: 'Rahul Tekdi',
firstName: 'Rahul ',
lastName: 'Tekdi',
orgIds: ['test-k12-channel']
},
identifier: 'do_21411914423214899211',
identifier: 'do_214151758052073472145',
authToken: ' ',
sid: 'iYO2K6dOSdA0rwq7NeT1TDzS-dbqduvV',
did: '7e85b4967aebd6704ba1f604f20056b6',
uid: 'bf020396-0d7b-436f-ae9f-869c6780fc45',
channel: '01269878797503692810',
channel: 'test-k12-channel',
pdata: {
id: 'dev.dock.portal',
ver: '2.8.0',
Expand Down Expand Up @@ -43,73 +43,12 @@ export const questionSetEditorConfig = {
l1: 'do_113140468925825024117',
l2: 'do_113140468926914560125'
},
host: 'https://staging.sunbirded.org',
host: '',
defaultLicense: 'CC BY 4.0',
endpoint: '/data/v3/telemetry',
env: 'questionset_editor',
framework: 'tn_k-12_5',
cloudStorageUrls: ['https://s3.ap-south-1.amazonaws.com/ekstep-public-qa/', 'https://ekstep-public-qa.s3-ap-south-1.amazonaws.com/',
'https://sunbirddev.blob.core.windows.net/sunbird-content-dev/', 'https://sunbirddevbbpublic.blob.core.windows.net/sunbird-content-staging/',
'https://obj.stage.sunbirded.org/sunbird-content-staging/'
],
board: 'CBSE',
medium: ['English'],
gradeLevel: ['Class 1'],
subject: ['Hindi'],
additionalCategories: [
{
value: 'Classroom Teaching Video',
label: 'Classroom Teaching Video'
},
{
value: 'Concept Map',
label: 'Concept Map'
},
{
value: 'Curiosity Question Set',
label: 'Curiosity Question Set'
},
{
value: 'Textbook',
label: 'Textbook'
},
{
value: 'Experiential Resource',
label: 'Experiential Resource'
},
{
value: 'Explanation Video',
label: 'Explanation Video'
},
{
value: 'Focus Spot',
label: 'Focus Spot'
},
{
value: 'Learning Outcome Definition',
label: 'Learning Outcome Definition'
},
{
value: 'Marking Scheme Rubric',
label: 'Marking Scheme Rubric'
},
{
value: 'Pedagogy Flow',
label: 'Pedagogy Flow'
},
{
value: 'Lesson Plan',
label: 'Lesson Plan'
},
{
value: 'Previous Board Exam Papers',
label: 'Previous Board Exam Papers'
},
{
value: 'TV Lesson',
label: 'TV Lesson'
}
],
framework: 'test_k12_framework',
cloudStorageUrls: ['https://knowlg-public.s3-ap-south-1.amazonaws.com/'],
labels: {
save_collection_btn_label: 'Save as Draft',
},
Expand Down Expand Up @@ -185,4 +124,4 @@ export const questionSetEditorConfig = {
assetProxyUrl: '/assets/public/',
commonFrameworkLicenseUrl: 'https://creativecommons.org/licenses/'
}
};
};
53 changes: 53 additions & 0 deletions src/pages/api/proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export default async function handler(req, res) {
const { method, query } = req;
const { path } = query;

// If the 'path' is an array, join it to form the correct endpoint path
const pathString = Array.isArray(path) ? path.join('/') : path;

// Construct the full URL using the environment variable BASE_URL
const BASE_URL = process.env.BASE_URL;
const API_KEY = process.env.AUTH_API_TOKEN;
const TENANT_ID = process.env.TENANT_ID;

if (pathString === '/action/data/v3/telemetry') {
return res.status(200).json({ message: 'Mocked Success - Skipping actual API call', data: {} });
}

// Append any existing query parameters from the original request
const queryString = req.url.includes('?') ? req.url.split('?')[1] : '';

// Build target URL
const targetUrl = `${BASE_URL}${pathString}${queryString ? `?${queryString}` : ''}`;
console.log('Target URL:', targetUrl); // To verify the final constructed URL

try {
// Prepare options for the proxied request
const options = {
method,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}`,
'tenantId': TENANT_ID,
},
};

// If the method is POST or PUT, include the body
if (method === 'POST' || method === 'PATCH') {
options.body = JSON.stringify(req.body); // Pass the request body
}

// Make the proxied request to the target URL
const response = await fetch(targetUrl, options);

// Parse response from the external API
const data = await response.json();

// Return the response back to the client
res.status(response.status).json(data);
} catch (error) {
console.error('Error in proxy:', error.message);
res.status(500).json({ message: 'Error fetching data', error: error.message });
}
}

0 comments on commit 25c33ec

Please sign in to comment.