Giter Club home page Giter Club logo

curp's Introduction

CURP

Verify CURP and obtain personal information from the Mexican government CURP.

Inspired by node-module-boilerplate

Installing

npm i get-mexican-data-by-curp

Express demo template

Express demo template

How to Start Using CURP package?

PUML

@startuml
class Client
class MexicanFinder {
   find(curp): Mexican
}
Client --> MexicanFinder
class Provider {
provide(curp): Mexican
}
MexicanFinder -right-> "1..*" Provider
Provider <|-down- Firestore
Provider <|-down- GovernmentScrapper
Provider <|-down- Arsus
@enduml

Getting started

import { GovernmentScrapper, CaptchaSolver, Arsus } from "get-mexican-data-by-curp";
import { Mexican } from "get-mexican-data-by-curp";
import { CaptchaSolver } from "get-mexican-data-by-curp";

const mexicanFinder = new MexicanFinder(
  // Your own database.
  new Firestore(),
  // Other providers: Arsus, Conectame, ...
  new Arsus("apiKey"),
  // 2Captcha ApiKey https://2captcha.com/enterpage
  new GovernmentScrapper(new CaptchaSolver("apiKey"))
);
const mexican: Mexican = await mexicanFinder.find(new Curp('CURP'));
if (mexicanFinder.finalState() !== 'Firestore') {
  // Save mexican
  new Firestore().save(mexican);
}

Your custom providers

Firestore example

import { getFirestore } from 'firebase-admin/lib/firestore';
import { Curp } from 'get-mexican-data-by-curp';
import { Provider } from 'get-mexican-data-by-curp';
import { GovernmentScrapperCache } from 'get-mexican-data-by-curp';
import { Mexican } from 'get-mexican-data-by-curp';

export class Firestore extends Provider implements GovernmentScrapperCache {
  constructor(
    private documentPath: (curpValue: string) => string = curpValue =>
      `id/${curpValue}`
  ) {
    super();
  }
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  save(mexican: Mexican): Promise<any> {
    if (mexican === undefined || mexican.curp === undefined) {
      throw new Error('Provider error');
    }
    return getFirestore().collection('id').doc(mexican.curp).set(mexican);
  }
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  provide(curp: Curp) {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
    return getFirestore()
      .doc(this.documentPath(curp.value))
      .get()
      .then((document: { exists: any; data: () => any }) => {
        if (!document.exists) {
          return null;
        }
        const data = document.data();
        // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
        if (data.error) {
          return {
            curp: curp.value,
            // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
            error: data.error,
          };
        }
        // eslint-disable-next-line @typescript-eslint/no-unsafe-return
        return data;
      });
  }
}

curp's People

Contributors

sanchezcarlosjr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.