Introduction to GitURL
GitURL is a powerful library for managing and manipulating Git URLs in your software projects. It provides a variety of APIs for parsing, validating, and transforming Git URLs seamlessly. Below is a detailed guide with practical examples to help you use GitURL effectively in your projects.
API Examples
1. Parsing Git URLs
const giturl = require('giturl');
const url = 'https://github.com/user/repo.git';
const parsedUrl = giturl.parse(url);
console.log(parsedUrl);
2. Validating Git URLs
const isValid = giturl.validate('https://github.com/user/repo.git');
console.log(isValid); // true or false
3. Converting Git URLs
const httpsUrl = giturl.toHttps('git@github.com:user/repo.git');
console.log(httpsUrl); // https://github.com/user/repo.git
const sshUrl = giturl.toSSH('https://github.com/user/repo.git');
console.log(sshUrl); // git@github.com:user/repo.git
4. Extracting Repository Information
const repoInfo = giturl.info('https://github.com/user/repo.git');
console.log(repoInfo);
// {owner: 'user', name: 'repo', type: 'github'}
5. URL Normalization
const normalizedUrl = giturl.normalize('https://github.com/user/repo.git');
console.log(normalizedUrl); // https://github.com/user/repo.git
const normalizedUrlSSH = giturl.normalize('git@github.com:user/repo.git');
console.log(normalizedUrlSSH); // https://github.com/user/repo.git
Practical Application Example
Building a URL Converter App
Let’s build a simple app that converts SSH Git URLs to HTTPS format using the GitURL library.
const express = require('express');
const giturl = require('giturl');
const app = express();
app.use(express.json());
app.post('/convert', (req, res) => {
const {sshUrl} = req.body;
const httpsUrl = giturl.toHttps(sshUrl);
res.json({httpsUrl});
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
With this application, you can easily convert SSH URLs to HTTPS by sending a POST request to the /convert endpoint with the SSH URL in the body.
Hash: 3136843009bc060ed24bcba7cba931d4ebc7e754a592a8bbbe31c4da33336460